-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: preserve EA dtype in transpose #30091
Changes from 3 commits
a2217e0
4fb44c5
e18a426
9be80b7
52d3b9c
bfdfccf
10d81bd
132472d
6aae8d6
5448bbb
9d703a2
8b8a464
79550bd
feecee8
dada1a6
1e58c4e
9abd6c2
f6b3c37
4675de6
6d9daa8
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 |
---|---|---|
|
@@ -721,12 +721,37 @@ def transpose(self, *args, **kwargs): | |
new_axes = self._construct_axes_dict_from( | ||
self, [self._get_axis(x) for x in axes_names] | ||
) | ||
new_values = self.values.transpose(axes_numbers) | ||
if kwargs.pop("copy", None) or (len(args) and args[-1]): | ||
new_values = new_values.copy() | ||
|
||
if ( | ||
self._is_homogeneous_type | ||
and len(self._data.blocks) | ||
and is_extension_array_dtype(self._data.blocks[0].dtype) | ||
): | ||
kwargs.pop("copy", None) # by definition, we're copying | ||
dtype = self._data.blocks[0].dtype | ||
arr_type = dtype.construct_array_type() | ||
|
||
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 move this logic to pandas/core/reshape/reshape.py this has a lot of similiarity to _unstack_extension_series |
||
# Slow, but unavoidable with 1D EAs. | ||
new_values = [] | ||
for i in range(len(self)): | ||
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'm rethinking this approach. This results in |
||
new_values.append( | ||
arr_type._from_sequence( | ||
[block.values[i] for block in self._data.blocks], dtype=dtype | ||
) | ||
) | ||
columns = new_axes.pop("columns") | ||
new_values = dict(zip(columns, new_values)) | ||
result = self._constructor(new_values, **new_axes) | ||
|
||
else: | ||
new_values = self.values.transpose(axes_numbers) | ||
if kwargs.pop("copy", None) or (len(args) and args[-1]): | ||
new_values = new_values.copy() | ||
|
||
result = self._constructor(new_values, **new_axes) | ||
|
||
nv.validate_transpose(tuple(), kwargs) | ||
return self._constructor(new_values, **new_axes).__finalize__(self) | ||
return result.__finalize__(self) | ||
|
||
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.
we can avoid self._data references by making this
len(self.dtypes) and is_extension_array_dtype(self.dtypes.iloc[0])
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.
ditto on 731 with self.dtypes