You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
groupby(multi-index level) not working correctly on a multi-indexed DataArray or DataSet #6836
changed the title [-]groupby not working correctly on a multi-indexed DataArray or DataSet[/-][+]groupby(multi-index level) not working correctly on a multi-indexed DataArray or DataSet[/+]on Jul 27, 2022
After trying to dig down further into the code, I saw that grouping over levels seems to be broken generally (up-to-date main branch at time of writing), i.e.
There is a lot of complexity related to pandas.MultiIndex special cases and it's been difficult to avoid new issues arising during the index refactor.
create_default_index_implicit has some hacks to create xarray objects directly from pandas.MultiIndex instances (e.g., xr.Dataset(coords={"x": pd_midx})) or even from xarray objects wrapping multi-indexes. The error raised here suggests that the issue should fixed before this call... Probably in safe_cast_to_index indeed.
We should probably avoid using .to_index() internally, or should we even deprecate it? The fact that mda.one.to_index() (in v2022.3.0) doesn't return the same result than mda.indexes["one"] adds more confusion than it adds value. Actually, in the long-term I'd be for deprecating all pandas.MultiIndex special cases in Xarray.
From #7282 it looks like we need to convert the multi-index level to a single index when casting the group to an index. And from #7105 we can fix that in safe_cast_to_index() (sometimes the full multi-index is expected) so we probably need a special case in groupby.
Is there hope for groupby working on multi-indexed DataArrays again in the future? We -- and from the issue history it looks like others too -- are currently pinning xarray<2022.6 even though we would love to use newer versions.
A special-case sounds reasonable to me as well as a temporary fix before looking into if/how we can refactor groupby so that it works with multiple kinds of built-in and/or custom indexes.
Activity
[-]groupby not working correctly on a multi-indexed DataArray or DataSet[/-][+]groupby(multi-index level) not working correctly on a multi-indexed DataArray or DataSet[/+]dcherian commentedon Aug 11, 2022
@benbovy I tracked this down to
We call
to_indexhere insafe_cast_to_index:xarray/xarray/core/utils.py
Lines 115 to 140 in f8fee90
Not sure if the fix should be only in the GroupBy specifically or more generally in
safe_cast_to_indexThe GroupBy context is
xarray/xarray/core/groupby.py
Line 434 in f8fee90
FabianHofmann commentedon Sep 22, 2022
After trying to dig down further into the code, I saw that grouping over levels seems to be broken generally (up-to-date main branch at time of writing), i.e.
raises:
in the function
create_default_index_implicit. I am still a bit puzzled how to approach this. Any idea @benbovy?benbovy commentedon Sep 22, 2022
Thanks @emmaai for the issue report and thanks @dcherian and @FabianHofmann for tracking it down.
There is a lot of complexity related to
pandas.MultiIndexspecial cases and it's been difficult to avoid new issues arising during the index refactor.create_default_index_implicithas some hacks to create xarray objects directly frompandas.MultiIndexinstances (e.g.,xr.Dataset(coords={"x": pd_midx})) or even from xarray objects wrapping multi-indexes. The error raised here suggests that the issue should fixed before this call... Probably insafe_cast_to_indexindeed.We should probably avoid using
.to_index()internally, or should we even deprecate it? The fact thatmda.one.to_index()(in v2022.3.0) doesn't return the same result thanmda.indexes["one"]adds more confusion than it adds value. Actually, in the long-term I'd be for deprecating allpandas.MultiIndexspecial cases in Xarray.xarray 2022.06.0 has a bug which breaks BrainIO: pydata/xarray#6836.
unpin xarray, numpy, pandas, netcdf4 (#25)
disallow xarray 2022.9.0
disallow xarray 2022.9.0 (#29)
benbovy commentedon Nov 14, 2022
From #7282 it looks like we need to convert the multi-index level to a single index when casting the group to an index. And from #7105 we can fix that in
safe_cast_to_index()(sometimes the full multi-index is expected) so we probably need a special case ingroupby.benbovy commentedon Nov 14, 2022
...we cannot fix that in
safe_cast_to_index()(or we can add a parameter to specify the desired result).mschrimpf commentedon Apr 5, 2023
Is there hope for groupby working on multi-indexed DataArrays again in the future? We -- and from the issue history it looks like others too -- are currently pinning
xarray<2022.6even though we would love to use newer versions.dcherian commentedon Apr 5, 2023
I think we could special-case extracting a multiindex level here:
xarray/xarray/core/groupby.py
Line 469 in d4db166
groupat that stage should have values@mschrimpf Can you try that and send in a PR?
benbovy commentedon Apr 12, 2023
A special-case sounds reasonable to me as well as a temporary fix before looking into if/how we can refactor groupby so that it works with multiple kinds of built-in and/or custom indexes.
emmaai commentedon Apr 13, 2023
I solved it temporarily by
reset_indextogroupbyandset_xindexafter, if anyone is looking.Fix .groupby(multi index level)
Fix .groupby(multi index level) (#7830)
carynbear commentedon Aug 20, 2024
This is happening again :(
max-sixty commentedon Aug 20, 2024
@carynbear please could you post a reproducible example? A new bug report would be ideal...