From 0b5def48f0ef54f521ba8ff6ad0183ed69d7c4f8 Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Sat, 28 Aug 2021 18:05:17 +0200 Subject: [PATCH 1/2] TST: added test for ea dtypes conversion to datetimetzdtype --- pandas/tests/frame/methods/test_astype.py | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index 398c66584390b..c30d5aaf02352 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -691,6 +691,38 @@ def test_astype_noncontiguous(self, index_slice): expected = df.iloc[index_slice] tm.assert_frame_equal(result, expected, check_dtype=False) + @pytest.mark.parametrize( + "dtype", + tm.ALL_INT_EA_DTYPES + tm.FLOAT_EA_DTYPES, + ) + def test_astype_ea_to_datetimetzdtype(self, dtype): + # GH37553 + result = Series([4, 0, 9], dtype=dtype).astype(DatetimeTZDtype(tz="US/Pacific")) + expected = Series( + { + 0: Timestamp("1969-12-31 16:00:00.000000004-08:00", tz="US/Pacific"), + 1: Timestamp("1969-12-31 16:00:00.000000000-08:00", tz="US/Pacific"), + 2: Timestamp("1969-12-31 16:00:00.000000009-08:00", tz="US/Pacific"), + } + ) + + if dtype in tm.FLOAT_EA_DTYPES: + expected = Series( + { + 0: Timestamp( + "1970-01-01 00:00:00.000000004-08:00", tz="US/Pacific" + ), + 1: Timestamp( + "1970-01-01 00:00:00.000000000-08:00", tz="US/Pacific" + ), + 2: Timestamp( + "1970-01-01 00:00:00.000000009-08:00", tz="US/Pacific" + ), + } + ) + + tm.assert_equal(result, expected) + class TestAstypeCategorical: def test_astype_from_categorical3(self): From 3391499767e579becbba7ff4ca1fa2fb47193090 Mon Sep 17 00:00:00 2001 From: lorenzophys Date: Sat, 28 Aug 2021 20:28:29 +0200 Subject: [PATCH 2/2] TST: code review, moved test in the right location, changed assert method --- pandas/tests/frame/methods/test_astype.py | 32 --------------------- pandas/tests/series/methods/test_astype.py | 33 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py index c30d5aaf02352..398c66584390b 100644 --- a/pandas/tests/frame/methods/test_astype.py +++ b/pandas/tests/frame/methods/test_astype.py @@ -691,38 +691,6 @@ def test_astype_noncontiguous(self, index_slice): expected = df.iloc[index_slice] tm.assert_frame_equal(result, expected, check_dtype=False) - @pytest.mark.parametrize( - "dtype", - tm.ALL_INT_EA_DTYPES + tm.FLOAT_EA_DTYPES, - ) - def test_astype_ea_to_datetimetzdtype(self, dtype): - # GH37553 - result = Series([4, 0, 9], dtype=dtype).astype(DatetimeTZDtype(tz="US/Pacific")) - expected = Series( - { - 0: Timestamp("1969-12-31 16:00:00.000000004-08:00", tz="US/Pacific"), - 1: Timestamp("1969-12-31 16:00:00.000000000-08:00", tz="US/Pacific"), - 2: Timestamp("1969-12-31 16:00:00.000000009-08:00", tz="US/Pacific"), - } - ) - - if dtype in tm.FLOAT_EA_DTYPES: - expected = Series( - { - 0: Timestamp( - "1970-01-01 00:00:00.000000004-08:00", tz="US/Pacific" - ), - 1: Timestamp( - "1970-01-01 00:00:00.000000000-08:00", tz="US/Pacific" - ), - 2: Timestamp( - "1970-01-01 00:00:00.000000009-08:00", tz="US/Pacific" - ), - } - ) - - tm.assert_equal(result, expected) - class TestAstypeCategorical: def test_astype_from_categorical3(self): diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 7fbdc455a8dcf..732d375d136d0 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -16,6 +16,7 @@ NA, Categorical, CategoricalDtype, + DatetimeTZDtype, Index, Interval, NaT, @@ -363,6 +364,38 @@ def test_astype_nan_to_bool(self): expected = Series(True, dtype="bool") tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "dtype", + tm.ALL_INT_EA_DTYPES + tm.FLOAT_EA_DTYPES, + ) + def test_astype_ea_to_datetimetzdtype(self, dtype): + # GH37553 + result = Series([4, 0, 9], dtype=dtype).astype(DatetimeTZDtype(tz="US/Pacific")) + expected = Series( + { + 0: Timestamp("1969-12-31 16:00:00.000000004-08:00", tz="US/Pacific"), + 1: Timestamp("1969-12-31 16:00:00.000000000-08:00", tz="US/Pacific"), + 2: Timestamp("1969-12-31 16:00:00.000000009-08:00", tz="US/Pacific"), + } + ) + + if dtype in tm.FLOAT_EA_DTYPES: + expected = Series( + { + 0: Timestamp( + "1970-01-01 00:00:00.000000004-08:00", tz="US/Pacific" + ), + 1: Timestamp( + "1970-01-01 00:00:00.000000000-08:00", tz="US/Pacific" + ), + 2: Timestamp( + "1970-01-01 00:00:00.000000009-08:00", tz="US/Pacific" + ), + } + ) + + tm.assert_series_equal(result, expected) + class TestAstypeString: @pytest.mark.parametrize(