Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ready for pandas 3 copy-on-write #8843

Closed
dcherian opened this issue Mar 15, 2024 · 2 comments · Fixed by #8846
Closed

Get ready for pandas 3 copy-on-write #8843

dcherian opened this issue Mar 15, 2024 · 2 comments · Fixed by #8846

Comments

@dcherian
Copy link
Contributor

What is your issue?

This line fails with pd.set_options("mode.copy_on_write", True)

assert all(obj.data.flags.writeable for obj in obj.variables.values())

We'll need to fix this before Pandas 3 is released in April:

assert all(obj.data.flags.writeable for obj in obj.variables.values())

Here's a test

def example():
    obj = Dataset()
    obj["dim2"] = ("dim2", 0.5 * np.arange(9))
    obj["time"] = ("time", pd.date_range("2000-01-01", periods=20)
    print({k: v.data.flags for k, v in obj.variables.items()})
    return obj

example()
pd.set_options("mode.copy_on_write", True)
example()
@dcherian dcherian changed the title Get ready for pandas copy-on-write Get ready for pandas 3 copy-on-write Mar 15, 2024
@phofl
Copy link
Contributor

phofl commented Mar 15, 2024

I would recommend to either copy the data they come from a pandas object or set the flags to writeable if you are ok with the implications or if you don't modify the data inplace anyway. That said, modifying the underlying data of an Index is a really bad idea if you or your users want to re-use the Index. That can lead to segfaults among other things

Be aware if you set the flag to writeable, this is not possible in all scenarios, e.g.

idx = pd.Index([1, 2, 3], dtype="int64[pyarrow]")
vals = idx.to_numpy()
vals.flags.writeable = True

This will raise, arrow protects against this and does not allow you to set the flag to writeable

@dcherian
Copy link
Contributor Author

Thanks @phofl I suspect this assertion is overly broad and we didn't want to do it for Index-backed Xarray variables.

dcherian added a commit to dcherian/xarray that referenced this issue Mar 16, 2024
dcherian added a commit that referenced this issue Mar 18, 2024
* Support pandas copy-on-write behaviour

Closes #8843

* Update xarray/tests/__init__.py

* One more fix

* Fix interp

* Avoid copy

* Try again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants