diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 80d9a22a39b1e..53d196c78d988 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 + 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):