From 4682fcad288197d588f57edd4a42d02db3c43088 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 21 May 2021 13:35:36 -0700 Subject: [PATCH 1/2] BUG: pydatetime case followup to #41555 --- pandas/core/dtypes/cast.py | 10 ++++++++-- pandas/tests/frame/test_constructors.py | 5 ++++- pandas/tests/series/test_constructors.py | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 80d9a22a39b1e..1bf2180489dc1 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1915,13 +1915,19 @@ def maybe_unbox_datetimelike_tz_deprecation( along with a timezone-naive datetime64 dtype, which is deprecated. """ # Caller is responsible for checking dtype.kind in ["m", "M"] + + if isinstance(value, datetime): + # we dont want to box dt64, in particular datetime64("NaT") + value = maybe_box_datetimelike(value, dtype) + try: value = maybe_unbox_datetimelike(value, dtype) except TypeError: if ( - isinstance(value, Timestamp) - and value.tz is not None + isinstance(value, datetime) + and value.tzinfo is not None and isinstance(dtype, np.dtype) + and dtype.kind == "M" ): warnings.warn( "Data is timezone-aware. Converting " diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index eee322e33bc8d..cca8c9283d552 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2474,10 +2474,13 @@ def test_construction_preserves_tzaware_dtypes(self, tz): ) tm.assert_series_equal(result, expected) - def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture): + @pytest.mark.parametrize("pydt", [True, False]) + def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt): # GH#25843, GH#41555, GH#33401 tz = tz_aware_fixture ts = Timestamp("2019", tz=tz) + if pydt: + ts = ts.to_pydatetime() ts_naive = Timestamp("2019") with tm.assert_produces_warning(FutureWarning): diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 41c0cbf58e438..8b1f268896240 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1535,10 +1535,13 @@ def test_constructor_tz_mixed_data(self): expected = Series(dt_list, dtype=object) tm.assert_series_equal(result, expected) - def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture): + @pytest.mark.parametrize("pydt", [True, False]) + def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt): # GH#25843, GH#41555, GH#33401 tz = tz_aware_fixture ts = Timestamp("2019", tz=tz) + if pydt: + ts = ts.to_pydatetime() ts_naive = Timestamp("2019") with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): From b174f932b75466530bbd9e5263d85ae45391d1bb Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 21 May 2021 16:37:34 -0700 Subject: [PATCH 2/2] mypy fixup --- pandas/core/dtypes/cast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 1bf2180489dc1..53d196c78d988 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1924,7 +1924,7 @@ def maybe_unbox_datetimelike_tz_deprecation( value = maybe_unbox_datetimelike(value, dtype) except TypeError: if ( - isinstance(value, datetime) + isinstance(value, Timestamp) and value.tzinfo is not None and isinstance(dtype, np.dtype) and dtype.kind == "M"