-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix+test dataframe tranpose with datetimeTZ #23730
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 3 commits
24d6783
22a4809
a76581f
7860d1f
d0de3d8
de94807
ed76af8
ec4cfeb
894ff2d
0f38a69
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 |
---|---|---|
|
@@ -939,3 +939,55 @@ def test_unstack_fill_frame_object(): | |
index=list('xyz') | ||
) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
def test_transpose_dt64tz(): | ||
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. Could you also test transposing a mixed dtype dataframe if we aren't already (e.g. datetimetz and int)? 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. Will do. Only real difference is that it doesn't round-trip (though I think that behavior is correct) 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. Yeah that makes sense. Just good to ensure that there's no aggressive coercion of other data dtypes in the DataFrame once transposed i.e. the columns in |
||
# GH#23730 transposing a DataFrame with a single datetime64tz column should | ||
# not raise ValueError | ||
|
||
dti = pd.date_range('1977-04-15', periods=3, freq='MS', tz='US/Hawaii') | ||
|
||
# For reasons unknown this error shows up differently depending on how the | ||
# DataFrame was constructed, so we do this several different ways. | ||
|
||
df1 = dti.to_series(keep_tz=True).to_frame() | ||
df2 = pd.DataFrame(dti, index=dti) | ||
df3 = pd.Series(dti, index=dti).to_frame() | ||
|
||
tm.assert_frame_equal(df1, df2) | ||
tm.assert_frame_equal(df2, df3) | ||
|
||
for frame in [df1, df2, df3]: | ||
frame.T | ||
tm.assert_frame_equal(frame.T.T, frame) | ||
|
||
# Now going the other direction, we have to manually construct the | ||
# transposed dataframe | ||
df = pd.DataFrame(np.arange(9).reshape(3, 3)) | ||
df[0] = dti[0] | ||
df[1] = dti[1] | ||
df[2] = dti[2] | ||
|
||
df.T | ||
tm.assert_frame_equal(df.T.T, df) | ||
|
||
|
||
def test_transpose_dt64tz_mixed_tz(): | ||
# GH#23730 transposing two datetimetz columns with different tzs | ||
dti = pd.date_range('1977-04-15', periods=3, freq='MS', tz='US/Hawaii') | ||
dti2 = pd.date_range('1977-04-15', periods=3, freq='MS', tz='UTC') | ||
|
||
df = pd.DataFrame({"A": dti, "B": dti2}, columns=["A", "B"]) | ||
df.T | ||
tm.assert_frame_equal(df.T.T, df.astype(object)) | ||
|
||
|
||
def test_transpose_dt64tz_mixed(): | ||
# GH#23730 transposing with datetimetz column and numeric column, | ||
# did not raise before but covering our bases | ||
|
||
dti = pd.date_range('1977-04-15', periods=3, freq='MS', tz='US/Hawaii') | ||
df = pd.DataFrame({"A": dti, "B": [3, 4, 5]}, columns=["A", "B"]) | ||
|
||
df.T | ||
tm.assert_frame_equal(df.T.T, df.astype(object)) |
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.
not sure why you should do this here at all. Move this to internals and just create new blocks, rather than hacking 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.
The point is that it is going to be a hack regardless unless we allow 2D EAs.
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.
not going to happen any time soon if ever
as i said this is way too hacky and needs to be pushed to internals