-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API, DEPR: Raise and Deprecate Reshape for Pandas Objects #13012
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
Closed
gfyoung
wants to merge
1
commit into
pandas-dev:master
from
forking-repos:categorical-reshape-validate
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1839,7 +1839,7 @@ def convert(self, *args, **kwargs): | |
try: | ||
values = values.reshape(shape) | ||
values = _block_shape(values, ndim=self.ndim) | ||
except AttributeError: | ||
except (AttributeError, NotImplementedError): | ||
pass | ||
newb = make_block(values, ndim=self.ndim, placement=[rl]) | ||
blocks.append(newb) | ||
|
@@ -3616,7 +3616,7 @@ def value_getitem(placement): | |
return value | ||
else: | ||
if value.ndim == self.ndim - 1: | ||
value = value.reshape((1,) + value.shape) | ||
value = _safe_reshape(value, (1,) + value.shape) | ||
|
||
def value_getitem(placement): | ||
return value | ||
|
@@ -4686,6 +4686,28 @@ def rrenamer(x): | |
_transform_index(right, rrenamer)) | ||
|
||
|
||
def _safe_reshape(arr, new_shape): | ||
""" | ||
If possible, reshape `arr` to have shape `new_shape`, | ||
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. ok |
||
with a couple of exceptions (see gh-13012): | ||
|
||
1) If `arr` is a Categorical or Index, `arr` will be | ||
returned as is. | ||
2) If `arr` is a Series, the `_values` attribute will | ||
be reshaped and returned. | ||
|
||
Parameters | ||
---------- | ||
arr : array-like, object to be reshaped | ||
new_shape : int or tuple of ints, the new shape | ||
""" | ||
if isinstance(arr, ABCSeries): | ||
arr = arr._values | ||
if not isinstance(arr, Categorical): | ||
arr = arr.reshape(new_shape) | ||
return arr | ||
|
||
|
||
def _transform_index(index, func): | ||
""" | ||
Apply function to all values found in index. | ||
|
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
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.
no need for this validation
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.
That contradicts what you said earlier here.
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.
a small function is fine - not 20 lines of treated code that has to be maintained
I would just pass it to numpy for validation (the actual values) instead; just don't do anything with it
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.
What do you mean pass it in through
numpy
for validation? They don't have a separate function for validating the shape AFAIK. The only way I could do it is by callingnp.reshape(Categorical.codes, shape)
, but that seems a little odd.I understand the concern about not wanting to maintain all of this validation code, so if there is no simple way to do it, I might just opt for no validation since this method is being deprecated anyways.
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.
why ? that's exactly what I mean
that's the only fall u need
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.
call
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.
Fair enough, done.