From 4a9f1130428873f66c9b42704cb583476ae0e1ec Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 12 Jun 2022 11:33:53 -0700 Subject: [PATCH] ENH: Timestamp.tz_convert support non-nano --- pandas/_libs/tslibs/timestamps.pyx | 5 ++--- pandas/tests/scalar/timestamp/test_timestamp.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index c6bae70d04a98..ff494afc3aeee 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -2074,8 +2074,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 @@ -2084,7 +2082,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 diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 89e5ce2241e42..70d589fef30cf 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -21,6 +21,7 @@ from pandas._libs.tslibs.timezones import ( dateutil_gettz as gettz, get_timezone, + tz_compare, ) import pandas.util._test_decorators as td @@ -850,3 +851,13 @@ def test_timestamp(self, dt64, ts): def test_to_period(self, dt64, ts): alt = Timestamp(dt64) assert ts.to_period("D") == alt.to_period("D") + + 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)