Skip to content

BUG: DatetimeIndex.astype matches Series.dtype with datetime tz dtype #33409

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Timezones
^^^^^^^^^

- Bug in :func:`to_datetime` with ``infer_datetime_format=True`` where timezone names (e.g. ``UTC``) would not be parsed correctly (:issue:`33133`)
-
- Bug in :func:`DatetimeIndex.astype` with a ``datetime64[ns, tz]`` dtype where the data was not localized to UTC first (:issue:`33401`)


Numeric
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ def astype(self, dtype, copy=True):
# GH#18951: datetime64_ns dtype but not equal means different tz
new_tz = getattr(dtype, "tz", None)
if getattr(self.dtype, "tz", None) is None:
return self.tz_localize(new_tz)
# GH 33401: Match the behavior of Series.astype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it obvious that Series is the better behavior? it might be that the only unambiguous thing to do is raise and require the user to be explicit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. The implicit UTC localization isn't my favorite behavior.

With that in mind, should we deprecate the Series.astype(tz_dtype) UTC localization behavior instead?

return self.tz_localize(timezones.UTC).tz_convert(new_tz)
result = self.tz_convert(new_tz)
if copy:
result = result.copy()
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ def test_astype_with_tz(self):
tm.assert_index_equal(result, expected)

# GH 18951: tz-naive to tz-aware
# GH 33401: Match the behavior of Series.astype
idx = date_range("20170101", periods=4)
result = idx.astype("datetime64[ns, US/Eastern]")
expected = date_range("20170101", periods=4, tz="US/Eastern")
expected = (
date_range("20170101", periods=4)
.tz_localize("UTC")
.tz_convert("US/Eastern")
)
tm.assert_index_equal(result, expected)

def test_astype_str_compat(self):
Expand Down Expand Up @@ -321,3 +326,13 @@ def test_astype_array_fallback(self, tz):
result = obj._data.astype(bool)
expected = np.array([True, True])
tm.assert_numpy_array_equal(result, expected)

def test_astype_tz_dtype_utc_localization(self):
# GH 33401
result = pd.Index([pd.Timestamp("2020-01-01 15:00")]).astype(
"datetime64[ns, US/Eastern]"
)
expected = pd.Index([pd.Timestamp("2020-01-01 10:00")]).tz_localize(
"US/Eastern"
)
tm.assert_index_equal(result, expected)