1import numpy as np 2import pytest 3 4from pandas import DataFrame, date_range 5import pandas._testing as tm 6 7 8class TestTranspose: 9 def test_transpose_tzaware_1col_single_tz(self): 10 # GH#26825 11 dti = date_range("2016-04-05 04:30", periods=3, tz="UTC") 12 13 df = DataFrame(dti) 14 assert (df.dtypes == dti.dtype).all() 15 res = df.T 16 assert (res.dtypes == dti.dtype).all() 17 18 def test_transpose_tzaware_2col_single_tz(self): 19 # GH#26825 20 dti = date_range("2016-04-05 04:30", periods=3, tz="UTC") 21 22 df3 = DataFrame({"A": dti, "B": dti}) 23 assert (df3.dtypes == dti.dtype).all() 24 res3 = df3.T 25 assert (res3.dtypes == dti.dtype).all() 26 27 def test_transpose_tzaware_2col_mixed_tz(self): 28 # GH#26825 29 dti = date_range("2016-04-05 04:30", periods=3, tz="UTC") 30 dti2 = dti.tz_convert("US/Pacific") 31 32 df4 = DataFrame({"A": dti, "B": dti2}) 33 assert (df4.dtypes == [dti.dtype, dti2.dtype]).all() 34 assert (df4.T.dtypes == object).all() 35 tm.assert_frame_equal(df4.T.T, df4) 36 37 @pytest.mark.parametrize("tz", [None, "America/New_York"]) 38 def test_transpose_preserves_dtindex_equality_with_dst(self, tz): 39 # GH#19970 40 idx = date_range("20161101", "20161130", freq="4H", tz=tz) 41 df = DataFrame({"a": range(len(idx)), "b": range(len(idx))}, index=idx) 42 result = df.T == df.T 43 expected = DataFrame(True, index=list("ab"), columns=idx) 44 tm.assert_frame_equal(result, expected) 45 46 def test_transpose_object_to_tzaware_mixed_tz(self): 47 # GH#26825 48 dti = date_range("2016-04-05 04:30", periods=3, tz="UTC") 49 dti2 = dti.tz_convert("US/Pacific") 50 51 # mixed all-tzaware dtypes 52 df2 = DataFrame([dti, dti2]) 53 assert (df2.dtypes == object).all() 54 res2 = df2.T 55 assert (res2.dtypes == [dti.dtype, dti2.dtype]).all() 56 57 def test_transpose_uint64(self, uint64_frame): 58 59 result = uint64_frame.T 60 expected = DataFrame(uint64_frame.values.T) 61 expected.index = ["A", "B"] 62 tm.assert_frame_equal(result, expected) 63 64 def test_transpose_float(self, float_frame): 65 frame = float_frame 66 dft = frame.T 67 for idx, series in dft.items(): 68 for col, value in series.items(): 69 if np.isnan(value): 70 assert np.isnan(frame[col][idx]) 71 else: 72 assert value == frame[col][idx] 73 74 # mixed type 75 index, data = tm.getMixedTypeDict() 76 mixed = DataFrame(data, index=index) 77 78 mixed_T = mixed.T 79 for col, s in mixed_T.items(): 80 assert s.dtype == np.object_ 81 82 def test_transpose_get_view(self, float_frame): 83 dft = float_frame.T 84 dft.values[:, 5:10] = 5 85 86 assert (float_frame.values[5:10] == 5).all() 87