Closed brianpm closed 3 months ago
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
Can we strip off much more from the example?
I see days
and days0
are quite different — can we make them more any more similar and still see the failure? Does it require using cftime?
This is maybe a more minimal example—it does not require cftime or times in general:
source = xr.DataArray(range(2), dims=["x"], coords=[["a", "b"]])
da = source.x
da = da.assign_coords(y=("x", ["c", "d"]), z=("x", ["e", "f"]))
da = da.set_index(x=["y", "z"])
da.unstack("x")
I think the issue relates to the fact that da.variable
is an IndexVariable
instead of a Variable
. I'd have to do more digging to see if there was a time that this worked.
The v2023.5.0
breakpoint in the original example is maybe a bit of a red herring in that it appears that dss.time.dt.daysinmonth
switched from returning a Variable
-backed DataArray to an IndexVariable
-backed DataArray at that time.
Thanks @spencerkclark -- that's a better minimal example and diagnosis. I couldn't figure out how to tell the difference between days
and days0
.
Just to confirm, I went through and tested this out quickly and Spencer's example does indeed fail in an older version as well.
It looks like in the particular case Brian presented dss.time
was an IndexVariable
which then was returned as a Variable
by dt.daysinmonth
in older (pre v2023.5.0
) versions. This allowed the code to work previously whereas now it's returned as an IndexVariable
and then fails because of that. So maybe a case of it accidentally working before.
Yup, this is consistent with what I found. I should clarify, I'm not sure if the current behavior is intentional—it would be nice if the minimal example (and your more real-world use-case) worked.
Thanks @spencerkclark !
I updated the title — feel free to refine a bit more
What happened?
using
unstack
on a DataArray generated using the.dt.daysinmonth
accessor withtime
as a multiIndex fails with a ValueError. The mysterious part is that when I build an "identical" DataArray starting from the.data
of that same array, it works as expected (see output of example code).I asked a colleague for help with this, and she said the attached code worked for older versions of xarray, but said it seems to be broken starting at 2023.5.0.
What did you expect to happen?
Expected to get a DataArray (
days0
) with dimensions ('year', 'month') with sizes (2, 12), which is what I get with the alternate DataArray (calleddays
).Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment