home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 190683531

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
190683531 MDU6SXNzdWUxOTA2ODM1MzE= 1132 groupby with datetime DataArray fails with `AttributeError` 3404817 closed 0     7 2016-11-21T11:00:57Z 2016-12-19T17:15:17Z 2016-12-19T17:11:57Z CONTRIBUTOR      

I want to group some data by Oct-May season of each year, i.e. [(Oct 2000 - May 2001), (Oct 2001 - May 2002), ...]. I.e. I do not want some DJF-like mean over all the data but one value for each year.

To achieve this, I construct a DataArray that maps the time steps of my dataset (time coordinate) to the start of each of such a season (values) and feed that to groupby. python def _get_oct_may_index(ds): dd = ds.time.to_index().to_pydatetime() labels = [] coords = [] for d in dd: if d.month <= 5: refyear = d.year - 1 elif d.month >= 10: refyear = d.year else: continue dref = datetime.datetime(refyear, 10, 1) labels.append(dref) coords.append(d) return xr.DataArray(labels, coords=dict(time=coords), dims='time', name='season_start')

I give it a custom name='season_start', so I end up in the last else of GroupBy.__init__, where my DataArray named group gets passed on to the function unique_value_groups. However, that function apparently expects a NumPy array, rather than a DataArray.

Please see this ipynb showing the error.

Proposed solution

So it turns out this can easily be fixed by changing line 226 from

unique_values, group_indices = unique_value_groups(group, sort=sort)

to

unique_values, group_indices = unique_value_groups(group.values, sort=sort)

Please see this other ipynb where the result is as expected.

Now is this a bug or am I abusing the code somehow?

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/1132/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

  • 0 rows from issues_id in issues_labels
  • 7 rows from issue in issue_comments
Powered by Datasette · Queries took 0.864ms · About: xarray-datasette