Skip to content

ENH: Timestamp.tz_convert support non-nano #47320

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
Jun 13, 2022
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
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2112,8 +2112,6 @@ default 'raise'
>>> pd.NaT.tz_convert(tz='Asia/Tokyo')
NaT
"""
if self._reso != NPY_FR_ns:
raise NotImplementedError(self._reso)

if self.tzinfo is None:
# tz naive, use tz_localize
Expand All @@ -2122,7 +2120,8 @@ default 'raise'
)
else:
# Same UTC timestamp, different time zone
out = Timestamp(self.value, tz=tz)
tz = maybe_get_tz(tz)
out = type(self)._from_value_and_reso(self.value, reso=self._reso, tz=tz)
if out is not NaT:
out._set_freq(self._freq) # avoid warning in constructor
return out
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pandas._libs.tslibs.timezones import (
dateutil_gettz as gettz,
get_timezone,
tz_compare,
)
from pandas.errors import OutOfBoundsDatetime
import pandas.util._test_decorators as td
Expand Down Expand Up @@ -763,6 +764,16 @@ def test_month_name(self, dt64, ts):
alt = Timestamp(dt64)
assert ts.month_name() == alt.month_name()

def test_tz_convert(self, ts):
ts = Timestamp._from_value_and_reso(ts.value, ts._reso, utc)

tz = pytz.timezone("US/Pacific")
result = ts.tz_convert(tz)

assert isinstance(result, Timestamp)
assert result._reso == ts._reso
assert tz_compare(result.tz, tz)

def test_repr(self, dt64, ts):
alt = Timestamp(dt64)

Expand Down