-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix regression: IndexVariable.copy(deep=True) casts dtype=U to object #3095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
262581f
Fix regression: IndexVariable.copy(deep=True) casts dtype=U to object
ab6960f
What's New
678e82b
Simplify copy()
crusaderky 3e7825c
Merge remote-tracking branch 'upstream/master' into object_index
ae212a0
Cosmetic
crusaderky 16e229d
Merge remote-tracking branch 'upstream/master' into object_index
da45c3b
Code review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite follow this comment -- how does pickling relate to this new method?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pandas.Index.copy(deep=False)
creates new identical views of the underlying numpy arrays.A numpy view becomes a real, deep-copied numpy array upon pickling. This is by design to prevent people from accidentally sending a huge numpy array over network or IPC when they just want to send a slice of it. Crucially though, this (IMHO improvable) design makes no distinction when the base array is the same size or smaller than the view.
Back to the method at hand: if two xarray.DataArray/Dataset share the same underlying pandas.Index, which is the whole intent of copy(deep=False), when they are pickled together they continue sharing the same data. This is already true when you shallow-copy xarray.Variable.
But if instead of
we wrote
which would be tempting as it is much more readable, everything would be the same initially, until you pickle the original index and its shallow copy together, at which point the copy would automatically and silently become a deep one, causing double RAM/disk usage upon storing/unpickling.
This was a real problem I faced a couple of years ago where I had to dump to disk (for forensic purposes) about 10,000 intermediate steps of a Monte Carlo simulation, each step being a DataArray or Dataset. The data variables were all dask-backed, so they were fine. But among the indices I had a 'scenario' dimension with 500,000 points, dtype='<U13'.
Before pickling: 13 * 500,000 = 6.2 MB; the 50,000 shallow copies of it being just views
After pickling: 13 * 500,000 * 10,000 = I needed a new hard disk!
This issue was from a couple of years ago and has since been fixed. My (apparently overcomplicated) code above prevents it from showing up again.
P.S. this, by the way, is a big problem with
xarray.align
when you are dealing with mixed numpy+dask arraysWatch the RAM usage of your dask client rocket to 8 GB, and several GBs worker-side too.
This will take a while because, client side, you just created 3.8 GB (500000 * 8 * 1000) worth of pickle file and are now sending it over the network.
[EDIT] nvm this last example. The initial concat() is already sending the RAM usage to 4 GB, meaning it's not calling
numpy.ndarray.broadcast_to
under the hood as it used to do...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation, this makes complete sense.