-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: transpose casts mixed dtypes to object #43340
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 9 commits
e33f12c
9c3ca03
142046f
badd4c1
14edad6
59cbed2
cf52e4d
fca34f9
a8b73ce
91521c4
56be13e
fb2fb3d
992c8cf
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 |
---|---|---|
|
@@ -3400,9 +3400,13 @@ def transpose(self, *args, copy: bool = False) -> DataFrame: | |
|
||
else: | ||
new_arr = self.values.T | ||
if copy: | ||
new_arr = new_arr.copy() | ||
result = self._constructor(new_arr, index=self.columns, columns=self.index) | ||
common_dtype = find_common_type(dtypes) if len(dtypes) > 0 else None | ||
result = self._constructor( | ||
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. pass copy to the constructor (and are you testing this)? |
||
new_arr, | ||
index=self.columns, | ||
columns=self.index, | ||
dtype=common_dtype, | ||
) | ||
|
||
return result.__finalize__(self, method="transpose") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ def test_transpose_tzaware_2col_mixed_tz(self): | |
df4 = DataFrame({"A": dti, "B": dti2}) | ||
assert (df4.dtypes == [dti.dtype, dti2.dtype]).all() | ||
assert (df4.T.dtypes == object).all() | ||
print(df4._can_fast_transpose, df4.T._can_fast_transpose) | ||
tm.assert_frame_equal(df4.T.T, df4) | ||
|
||
@pytest.mark.parametrize("tz", [None, "America/New_York"]) | ||
|
@@ -57,6 +58,7 @@ def test_transpose_object_to_tzaware_mixed_tz(self): | |
df2 = DataFrame([dti, dti2]) | ||
assert (df2.dtypes == object).all() | ||
res2 = df2.T | ||
print("\n", res2.dtypes, [dti.dtype, dti2.dtype]) | ||
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. remove prints 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. removed the prints |
||
assert (res2.dtypes == [dti.dtype, dti2.dtype]).all() | ||
|
||
def test_transpose_uint64(self, uint64_frame): | ||
|
@@ -103,3 +105,10 @@ def test_transpose_get_view_dt64tzget_view(self): | |
|
||
rtrip = result._mgr.blocks[0].values | ||
assert np.shares_memory(arr._data, rtrip._data) | ||
|
||
def test_transpose_mixed_dtypes(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. test the copy keyword. test an example which does keep object (e.g. (1, 1.5) and ( 'a', 1)) for (a, b) and test with simple dtypes as well (e.g. int64) |
||
# GH#43337 | ||
df = DataFrame({"a": [1], "b": [2]}).astype({"b": "Int64"}) | ||
result = df.T | ||
expected = DataFrame([1, 2], index=["a", "b"], dtype="Int64") | ||
tm.assert_frame_equal(result, expected) |
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.
this is saying the opposite of what you are doing. make this more clear to a reader