Skip to content

Commit 38a7d29

Browse files
authored
ENH: Timestamp.tz_convert support non-nano (#47320)
1 parent a8d8ae7 commit 38a7d29

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,8 +2121,6 @@ default 'raise'
21212121
>>> pd.NaT.tz_convert(tz='Asia/Tokyo')
21222122
NaT
21232123
"""
2124-
if self._reso != NPY_FR_ns:
2125-
raise NotImplementedError(self._reso)
21262124

21272125
if self.tzinfo is None:
21282126
# tz naive, use tz_localize
@@ -2131,7 +2129,8 @@ default 'raise'
21312129
)
21322130
else:
21332131
# Same UTC timestamp, different time zone
2134-
out = Timestamp(self.value, tz=tz)
2132+
tz = maybe_get_tz(tz)
2133+
out = type(self)._from_value_and_reso(self.value, reso=self._reso, tz=tz)
21352134
if out is not NaT:
21362135
out._set_freq(self._freq) # avoid warning in constructor
21372136
return out

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from pandas._libs.tslibs.timezones import (
2323
dateutil_gettz as gettz,
2424
get_timezone,
25+
tz_compare,
2526
)
2627
from pandas.errors import OutOfBoundsDatetime
2728
import pandas.util._test_decorators as td
@@ -763,6 +764,16 @@ def test_month_name(self, dt64, ts):
763764
alt = Timestamp(dt64)
764765
assert ts.month_name() == alt.month_name()
765766

767+
def test_tz_convert(self, ts):
768+
ts = Timestamp._from_value_and_reso(ts.value, ts._reso, utc)
769+
770+
tz = pytz.timezone("US/Pacific")
771+
result = ts.tz_convert(tz)
772+
773+
assert isinstance(result, Timestamp)
774+
assert result._reso == ts._reso
775+
assert tz_compare(result.tz, tz)
776+
766777
def test_repr(self, dt64, ts):
767778
alt = Timestamp(dt64)
768779

0 commit comments

Comments
 (0)