home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 722168932

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
722168932 MDU6SXNzdWU3MjIxNjg5MzI= 4513 where should keep_attrs be set in groupby, resample, weighted etc.? 10194086 closed 0     2 2020-10-15T09:36:43Z 2023-11-10T16:58:35Z 2023-11-10T16:58:35Z MEMBER      

I really should not open this can of worms but per https://github.com/pydata/xarray/issues/4450#issuecomment-697507489:

I'm always confused about whether ds.groupby(..., keep_attrs=True).mean() or ds.groupby(...).mean(keep_attrs=True) is correct. (similarly for rolling, coarsen etc.)

Also as I try to fix the keep_attr behavior in #4510 it would be good to know where they should go. So I tried to figure out how this is currently handled and found the following:

ds.xxx(keep_attrs=True).yyy() - all fixed

ds.xxx().yyy(keep_attrs=True) - coarsen (fixed in #5227) - groupby - groupby_bin - resample - rolling (adjusted in #4510) - rolling_exp (fixed in #4592) - weighted

So the working consensus seems to be to to ds.xxx().yyy(keep_attrs=True) - any comments on that?

(Edit: looking at this it is only half as bad, "only" coarsen, rolling (#4510), and rolling_exp would need to be fixed.)

Detailed analysis

```python import xarray as xr ds = xr.tutorial.open_dataset("air_temperature") da = ds.air ``` ### coarsen ```python ds.coarsen(time=2, keep_attrs=True).mean() # keeps global attributes ds.coarsen(time=2).mean(keep_attrs=True) # keeps DataArray attributes ds.coarsen(time=2, keep_attrs=True).mean(keep_attrs=True) # keeps both da.coarsen(time=2).mean(keep_attrs=True) # error da.coarsen(time=2, keep_attrs=True).mean() # keeps DataArray attributes ``` ### groupby ```python ds.groupby("time.month").mean(keep_attrs=True) # keeps both da.groupby("time.month").mean(keep_attrs=True) # keeps DataArray attributes ds.groupby("time.month", keep_attrs=True).mean() # error da.groupby("time.month", keep_attrs=True).mean() # error ``` ### groupby_bins ```python ds.groupby_bins(ds.lat, np.arange(0, 90, 10)).mean(keep_attrs=True) # keeps both da.groupby_bins(ds.lat, np.arange(0, 90, 10)).mean(keep_attrs=True) # keeps DataArray attrs ds.groupby_bins(ds.lat, np.arange(0, 90, 10), keep_attrs=True) # errors da.groupby_bins(ds.lat, np.arange(0, 90, 10), keep_attrs=True) # errors ``` ### resample ```python ds.resample(time="A").mean(keep_attrs=True) # keeps both da.resample(time="A").mean(keep_attrs=True) # keeps DataArray attributes ds.resample(time="A", keep_attrs=False).mean() # ignored da.resample(time="A", keep_attrs=False).mean() # ignored ``` ### rolling ```python ds.rolling(time=2).mean(keep_attrs=True) # keeps both da.rolling(time=2).mean(keep_attrs=True) # keeps DataArray attributes ds.rolling(time=2, keep_attrs=True).mean() # DeprecationWarning; keeps both da.rolling(time=2, keep_attrs=True).mean() # DeprecationWarning; keeps DataArray attributes ``` see #4510 ### rolling_exp ```python ds.rolling_exp(time=5, keep_attrs=True).mean() # ignored da.rolling_exp(time=5, keep_attrs=True).mean() # ignored ds.rolling_exp(time=5).mean(keep_attrs=True) # keeps both da.rolling_exp(time=5).mean(keep_attrs=True) # keeps DataArray attributes ``` ### weighted ```python ds.weighted(ds.lat).mean(keep_attrs=True) # keeps both da.weighted(ds.lat).mean(keep_attrs=True) # keeps DataArray attrs ```

edit: moved rolling after #4510, moved rolling_exp after #4592 and coarsen after #5227

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/4513/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 1 row from issues_id in issues_labels
  • 1 row from issue in issue_comments
Powered by Datasette · Queries took 160.891ms · About: xarray-datasette