Skip to content

BUG: pydatetime case followup to #41555 #41609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 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
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