-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: retain extension dtypes in transpose #28048
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
Changes from 2 commits
8e2bbee
fa775bd
ae6b966
069ddcc
29c2453
c812c69
80c4ee9
d70f4f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -725,7 +725,17 @@ def transpose(self, *args, **kwargs): | |
new_values = new_values.copy() | ||
|
||
nv.validate_transpose(tuple(), kwargs) | ||
return self._constructor(new_values, **new_axes).__finalize__(self) | ||
result = self._constructor(new_values, **new_axes).__finalize__(self) | ||
|
||
if self.ndim == 2 and self._is_homogeneous_type and len(self.columns): | ||
if is_extension_array_dtype(self.dtypes.iloc[0]): | ||
# Retain ExtensionArray dtypes through transpose; | ||
# TODO: this can be made cleaner if/when (N, 1) EA are allowed | ||
dtype = self.dtypes.iloc[0] | ||
for col in result.columns: | ||
result[col] = result[col].astype(dtype) | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
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. this pattern is very unusual, we normally don't use repeated setting like this. rather we create an array / dict in a comprehension then construct. for EA this might be ok (as we have a single EA per block), but would like not to use this patter anywhere 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. I would also override transpose in frame.py rather than do it here (and maybe just move the current one to Series) as they don't really share code. 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. Agreed this is sub-optimal. Ideally this will be made unnecessary by 2D EAs before too long. 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. Does 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. I dont understand the question; 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. Whoops, I meant 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. good idea. updated |
||
|
||
return result | ||
|
||
def swapaxes(self, axis1, axis2, copy=True): | ||
""" | ||
|
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 think its worthwhile to make a method to encapsulate this maybe