diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 16987d4e4492f7..7147e004330404 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1909,13 +1909,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 f04809d9d8832c..d271cf99b165cf 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2509,10 +2509,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 41c0cbf58e438d..8b1f268896240d 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):