Skip to content

Commit

Permalink
BUG: pydatetime case followup to pandas-dev#41555
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed May 21, 2021
1 parent 751d500 commit 4682fca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4682fca

Please sign in to comment.