-
-
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 8 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 |
---|---|---|
|
@@ -2503,7 +2503,7 @@ def memory_usage(self, index=True, deep=False): | |
) | ||
return result | ||
|
||
def transpose(self, *args, **kwargs): | ||
def transpose(self, *args, copy=False): | ||
""" | ||
Transpose index and columns. | ||
|
||
|
@@ -2513,9 +2513,14 @@ def transpose(self, *args, **kwargs): | |
|
||
Parameters | ||
---------- | ||
*args, **kwargs | ||
Additional arguments and keywords have no effect but might be | ||
accepted for compatibility with numpy. | ||
*args : tuple, optional | ||
Accepted for compatibility with NumPy. | ||
copy : bool, default False | ||
Whether to copy the data after transposing, even for DataFrames | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with a single dtype. | ||
|
||
Note that a copy is always required for mixed dtype DataFrames, | ||
or for DataFrames with any extension types. | ||
|
||
Returns | ||
------- | ||
|
@@ -2596,7 +2601,27 @@ def transpose(self, *args, **kwargs): | |
dtype: object | ||
""" | ||
nv.validate_transpose(args, dict()) | ||
return super().transpose(1, 0, **kwargs) | ||
# construct the args | ||
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. don't you think this is better located in pandas/core/reshape ? (and called as a helper function here) 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. Maybe a slight preference for keeping it here just for readability. The reshape part is essentially just a list comprehension. values = self.values
new_values = [arr_type._from_sequence(row, dtype=dtype) for row in values] which I don't think warrants its own function. I don't see anything places in But happy to move it if you want / if you see other places that could use parts of this. |
||
|
||
if self._is_homogeneous_type and is_extension_array_dtype(self.iloc[:, 0]): | ||
dtype = self.dtypes.iloc[0] | ||
arr_type = dtype.construct_array_type() | ||
values = self.values | ||
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. IIRC the single-column case could be done without this casting. think its worth special-casing? 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 don't think it can be done without casting in general. We'll still need to reshape the 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. im pretty sure i did this in the other PR, something like:
(of course, if we had 2D EAs...) 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. So, we can avoid casting to an ndarray by making My prior expectation is that converting to an ndarray and doing 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. what is 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. Number of columns. 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. right, i was specifically referring to single-column 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 OK with not-doing this optimization here, just want to make sure we're on the same page about what the available optimization is |
||
|
||
new_values = [arr_type._from_sequence(row, dtype=dtype) for row in values] | ||
result = self._constructor( | ||
dict(zip(self.index, new_values)), index=self.columns | ||
) | ||
|
||
else: | ||
new_values = self.values.T | ||
if copy: | ||
new_values = new_values.copy() | ||
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. if non-homogeneous, then 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. Are we 100% sure about that? Or are there types distinct dtypes that 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. if non-homogeneous then we have multiple blocks, so multiple ndarrays that are going through |
||
result = self._constructor( | ||
new_values, index=self.columns, columns=self.index | ||
) | ||
|
||
return result.__finalize__(self) | ||
|
||
T = property(transpose) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -644,50 +644,6 @@ def _set_axis(self, axis, labels): | |
self._data.set_axis(axis, labels) | ||
self._clear_item_cache() | ||
|
||
def transpose(self, *args, **kwargs): | ||
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 Series have transpose for compat? 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. Yes, via IndexOpsMixin. |
||
""" | ||
Permute the dimensions of the %(klass)s | ||
|
||
Parameters | ||
---------- | ||
args : %(args_transpose)s | ||
copy : bool, default False | ||
Make a copy of the underlying data. Mixed-dtype data will | ||
always result in a copy | ||
**kwargs | ||
Additional keyword arguments will be passed to the function. | ||
|
||
Returns | ||
------- | ||
y : same as input | ||
|
||
Examples | ||
-------- | ||
>>> p.transpose(2, 0, 1) | ||
>>> p.transpose(2, 0, 1, copy=True) | ||
""" | ||
|
||
# construct the args | ||
axes, kwargs = self._construct_axes_from_arguments( | ||
args, kwargs, require_all=True | ||
) | ||
axes_names = tuple(self._get_axis_name(axes[a]) for a in self._AXIS_ORDERS) | ||
axes_numbers = tuple(self._get_axis_number(axes[a]) for a in self._AXIS_ORDERS) | ||
|
||
# we must have unique axes | ||
if len(axes) != len(set(axes)): | ||
raise ValueError(f"Must specify {self._AXIS_LEN} unique axes") | ||
|
||
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() | ||
|
||
nv.validate_transpose(tuple(), kwargs) | ||
return self._constructor(new_values, **new_axes).__finalize__(self) | ||
|
||
def swapaxes(self, axis1, axis2, copy=True): | ||
""" | ||
Interchange axes and swap values axes appropriately. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -891,6 +891,22 @@ def test_no_warning(self, all_arithmetic_operators): | |
|
||
|
||
class TestTranspose: | ||
@pytest.mark.parametrize( | ||
"ser", | ||
[ | ||
pd.date_range("2016-04-05 04:30", periods=3, tz="UTC"), | ||
pd.period_range("1994", freq="A", periods=3), | ||
pd.period_range("1969", freq="9s", periods=1), | ||
pd.date_range("2016-04-05 04:30", periods=3).astype("category"), | ||
pd.date_range("2016-04-05 04:30", periods=3, tz="UTC").astype("category"), | ||
], | ||
) | ||
def test_transpose_retains_extension_dtype(self, ser): | ||
# case with more than 1 column, must have same dtype | ||
df = pd.DataFrame({"a": ser, "b": ser}) | ||
result = df.T | ||
assert (result.dtypes == ser.dtype).all() | ||
|
||
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.
? 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. Interestingly, the I've xfalied it for now, and likely won't have time to look into it. 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. sounds good, thanks |
||
def test_transpose_tzaware_1col_single_tz(self): | ||
# GH#26825 | ||
dti = pd.date_range("2016-04-05 04:30", periods=3, tz="UTC") | ||
|
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.
don't we still need kwargs for this?
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.
AFAICT, no. Neither
np.transpose
norndarray.transpose
take additional keyword arguments.