Skip to content

Commit 4682fca

Browse files
committed
BUG: pydatetime case followup to pandas-dev#41555
1 parent 751d500 commit 4682fca

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

pandas/core/dtypes/cast.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,13 +1915,19 @@ def maybe_unbox_datetimelike_tz_deprecation(
19151915
along with a timezone-naive datetime64 dtype, which is deprecated.
19161916
"""
19171917
# Caller is responsible for checking dtype.kind in ["m", "M"]
1918+
1919+
if isinstance(value, datetime):
1920+
# we dont want to box dt64, in particular datetime64("NaT")
1921+
value = maybe_box_datetimelike(value, dtype)
1922+
19181923
try:
19191924
value = maybe_unbox_datetimelike(value, dtype)
19201925
except TypeError:
19211926
if (
1922-
isinstance(value, Timestamp)
1923-
and value.tz is not None
1927+
isinstance(value, datetime)
1928+
and value.tzinfo is not None
19241929
and isinstance(dtype, np.dtype)
1930+
and dtype.kind == "M"
19251931
):
19261932
warnings.warn(
19271933
"Data is timezone-aware. Converting "

pandas/tests/frame/test_constructors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2474,10 +2474,13 @@ def test_construction_preserves_tzaware_dtypes(self, tz):
24742474
)
24752475
tm.assert_series_equal(result, expected)
24762476

2477-
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture):
2477+
@pytest.mark.parametrize("pydt", [True, False])
2478+
def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
24782479
# GH#25843, GH#41555, GH#33401
24792480
tz = tz_aware_fixture
24802481
ts = Timestamp("2019", tz=tz)
2482+
if pydt:
2483+
ts = ts.to_pydatetime()
24812484
ts_naive = Timestamp("2019")
24822485

24832486
with tm.assert_produces_warning(FutureWarning):

pandas/tests/series/test_constructors.py

Lines changed: 4 additions & 1 deletion
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)