Skip to content

Commit 9175eac

Browse files
authored
BUG: pydatetime case followup to #41555 (#41609)
1 parent a68523b commit 9175eac

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

pandas/core/dtypes/cast.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1909,13 +1909,19 @@ def maybe_unbox_datetimelike_tz_deprecation(
19091909
along with a timezone-naive datetime64 dtype, which is deprecated.
19101910
"""
19111911
# Caller is responsible for checking dtype.kind in ["m", "M"]
1912+
1913+
if isinstance(value, datetime):
1914+
# we dont want to box dt64, in particular datetime64("NaT")
1915+
value = maybe_box_datetimelike(value, dtype)
1916+
19121917
try:
19131918
value = maybe_unbox_datetimelike(value, dtype)
19141919
except TypeError:
19151920
if (
19161921
isinstance(value, Timestamp)
1917-
and value.tz is not None
1922+
and value.tzinfo is not None
19181923
and isinstance(dtype, np.dtype)
1924+
and dtype.kind == "M"
19191925
):
19201926
warnings.warn(
19211927
"Data is timezone-aware. Converting "

pandas/tests/frame/test_constructors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2509,10 +2509,13 @@ def test_construction_preserves_tzaware_dtypes(self, tz):
25092509
)
25102510
tm.assert_series_equal(result, expected)
25112511

2512-
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
2512+
@pytest.mark.parametrize("pydt", [True, False])
2513+
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
25132514
# GH#25843, GH#41555, GH#33401
25142515
tz = tz_aware_fixture
25152516
ts = Timestamp("2019", tz=tz)
2517+
if pydt:
2518+
ts = ts.to_pydatetime()
25162519
ts_naive = Timestamp("2019")
25172520

25182521
with tm.assert_produces_warning(FutureWarning):

pandas/tests/series/test_constructors.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,13 @@ def test_constructor_tz_mixed_data(self):
15351535
expected = Series(dt_list, dtype=object)
15361536
tm.assert_series_equal(result, expected)
15371537

1538-
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
1538+
@pytest.mark.parametrize("pydt", [True, False])
1539+
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
15391540
# GH#25843, GH#41555, GH#33401
15401541
tz = tz_aware_fixture
15411542
ts = Timestamp("2019", tz=tz)
1543+
if pydt:
1544+
ts = ts.to_pydatetime()
15421545
ts_naive = Timestamp("2019")
15431546

15441547
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):

0 commit comments

Comments
 (0)