From a6150b9b8d42a2a6ddf2a8fbc86ebc562fefe068 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 29 Oct 2019 16:08:50 +0000 Subject: [PATCH 1/3] Add simple test for GH 28448 --- pandas/tests/extension/test_categorical.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index e70e4f2fe501b..6143652b3ee47 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -223,6 +223,28 @@ def test_cast_category_to_extension_dtype(self, expected): tm.assert_series_equal(result, expected) + @pytest.mark.parametrize( + "data, dtype, expected", + [ + ( + "2015-01-01", + "datetime64[ns]", + np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"), + ), + ( + "2015-01-01", + "datetime64[ns, MET]", + np.array( + [Timestamp("2015-01-01 00:00:00+0100", tz="MET")], dtype=object + ), + ), + ], + ) + def test_consistent_casting(self, data, dtype, expected): + # GH 28448 + result = np.asarray(pd.Categorical(data).astype(dtype)) + assert result == expected + class TestArithmeticOps(base.BaseArithmeticOpsTests): def test_arith_series_with_scalar(self, data, all_arithmetic_operators): From d82930918b65334cbab92599fe2cf59b16d6aab5 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Wed, 30 Oct 2019 08:11:55 +0000 Subject: [PATCH 2/3] Update test so there is no np.asarray, but rather the original return types (datetimearray and np.array) are used --- pandas/tests/extension/test_categorical.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index 6143652b3ee47..c92e99b674261 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -224,25 +224,23 @@ def test_cast_category_to_extension_dtype(self, expected): tm.assert_series_equal(result, expected) @pytest.mark.parametrize( - "data, dtype, expected", + "dtype, expected", [ ( - "2015-01-01", "datetime64[ns]", np.array(["2015-01-01T00:00:00.000000000"], dtype="datetime64[ns]"), ), ( - "2015-01-01", "datetime64[ns, MET]", - np.array( - [Timestamp("2015-01-01 00:00:00+0100", tz="MET")], dtype=object + pd.arrays.DatetimeArray( + pd.array([pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")]) ), ), ], ) - def test_consistent_casting(self, data, dtype, expected): + def test_consistent_casting(self, dtype, expected): # GH 28448 - result = np.asarray(pd.Categorical(data).astype(dtype)) + result = pd.Categorical("2015-01-01").astype(dtype) assert result == expected From 0be007fb44ab3b714b7e4d00150a14c60469d29b Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Thu, 31 Oct 2019 10:40:48 +0000 Subject: [PATCH 3/3] Write expected output in test more succinctly --- pandas/tests/extension/test_categorical.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index c92e99b674261..528053aa8c7f1 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -232,9 +232,9 @@ def test_cast_category_to_extension_dtype(self, expected): ), ( "datetime64[ns, MET]", - pd.arrays.DatetimeArray( - pd.array([pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")]) - ), + pd.DatetimeIndex( + [pd.Timestamp("2015-01-01 00:00:00+0100", tz="MET")] + ).array, ), ], )