-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CoW: Track references in unstack if there is no copy #57487
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 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c504935
CoW: Track references in unstack if there is no copy
phofl 19dcda6
Update
phofl 1572bdb
Update
phofl a24201b
Update
phofl 0b0f17c
Merge branch 'main' into cow_unstack
phofl 5f25c02
Merge branch 'main' into cow_unstack
phofl 1657e5e
Merge branch 'main' into cow_unstack
mroeschke 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
factorize, | ||
unique, | ||
) | ||
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray | ||
from pandas.core.arrays.categorical import factorize_from_iterable | ||
from pandas.core.construction import ensure_wrapped_if_datetimelike | ||
from pandas.core.frame import DataFrame | ||
|
@@ -229,20 +230,33 @@ def arange_result(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.bool_]]: | |
return new_values, mask.any(0) | ||
# TODO: in all tests we have mask.any(0).all(); can we rely on that? | ||
|
||
def get_result(self, values, value_columns, fill_value) -> DataFrame: | ||
def get_result(self, obj, value_columns, fill_value) -> DataFrame: | ||
values = obj._values | ||
if values.ndim == 1: | ||
values = values[:, np.newaxis] | ||
|
||
if value_columns is None and values.shape[1] != 1: # pragma: no cover | ||
raise ValueError("must pass column labels for multi-column data") | ||
|
||
values, _ = self.get_new_values(values, fill_value) | ||
new_values, _ = self.get_new_values(values, fill_value) | ||
columns = self.get_new_columns(value_columns) | ||
index = self.new_index | ||
|
||
return self.constructor( | ||
values, index=index, columns=columns, dtype=values.dtype | ||
result = self.constructor( | ||
new_values, index=index, columns=columns, dtype=new_values.dtype, copy=False | ||
) | ||
if isinstance(values, np.ndarray): | ||
base, new_base = values.base, new_values.base | ||
elif isinstance(values, NDArrayBackedExtensionArray): | ||
base, new_base = values._ndarray.base, new_values._ndarray.base | ||
else: | ||
base, new_base = 1, 2 # type: ignore[assignment] | ||
if base is new_base: | ||
# We can only get here if one of the dimensions is size 1 | ||
mgr = result._mgr | ||
mgr.blocks[0].refs = obj._mgr.blocks[0].refs | ||
mgr.blocks[0].refs.add_reference(mgr.blocks[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah thanks, I was looking for this one but couldn't find it (was looking in the wrong place..) |
||
return result | ||
|
||
def get_new_values(self, values, fill_value=None): | ||
if values.ndim == 1: | ||
|
@@ -532,9 +546,7 @@ def unstack( | |
unstacker = _Unstacker( | ||
obj.index, level=level, constructor=obj._constructor_expanddim, sort=sort | ||
) | ||
return unstacker.get_result( | ||
obj._values, value_columns=None, fill_value=fill_value | ||
) | ||
return unstacker.get_result(obj, value_columns=None, fill_value=fill_value) | ||
|
||
|
||
def _unstack_frame( | ||
|
@@ -550,7 +562,7 @@ def _unstack_frame( | |
return obj._constructor_from_mgr(mgr, axes=mgr.axes) | ||
else: | ||
return unstacker.get_result( | ||
obj._values, value_columns=obj.columns, fill_value=fill_value | ||
obj, value_columns=obj.columns, fill_value=fill_value | ||
) | ||
|
||
|
||
|
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.
Are we sure this base checking always works? (compared to doing something like
np.shares_memory
)From some quick tests, it seems that even if you chain multiple no-copy transformations (like we do with
reshape().swapaxes().reshape()
), the base object stays the same.If the base object is backed by something non-numpy (eg parrow), this doesn't seem to be the case, but, I think in a DataFrame we probably always end up with an array with a base object that is another numpy array.
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.
we reshape into a 3dim array in between, so anything that is 1d copies there
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 was trying to come up with a (contrived) example where the base might be different:
So in the above after reshaping the base is not equal to the calling array's base.
However, when putting this array in a DataFrame and getting
_values
, it seems we still get the array wrapped in another array, so the direct base is again a numpy array: