From 5cd6d69e8635bb6efa670820f43aaccbb982a3f1 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 27 Dec 2021 16:14:12 -0800 Subject: [PATCH 1/3] BUG+DEPR: Timestamp.fromtimestamp tz arg --- pandas/_libs/tslibs/timestamps.pyx | 15 ++++++++++-- .../tests/scalar/timestamp/test_timestamp.py | 24 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 1c26793876e5a..307b6f2949881 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1164,10 +1164,20 @@ class Timestamp(_Timestamp): >>> pd.Timestamp.utcfromtimestamp(1584199972) Timestamp('2020-03-14 15:32:52') """ + # GH#22451 + warnings.warn( + "The behavior of Timestamp.utcfromtimestamp is deprecated, in a " + "future version will return a timezone-aware Timestamp with UTC " + "timezone. To keep the old behavior, use " + "Timestamp.utcfromtimestamp(ts).tz_localize(None). " + "To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')", + FutureWarning, + stacklevel=1, + ) return cls(datetime.utcfromtimestamp(ts)) @classmethod - def fromtimestamp(cls, ts): + def fromtimestamp(cls, ts, tz=None): """ Timestamp.fromtimestamp(ts) @@ -1180,7 +1190,8 @@ class Timestamp(_Timestamp): Note that the output may change depending on your local time. """ - return cls(datetime.fromtimestamp(ts)) + tz = maybe_get_tz(tz) + return cls(datetime.fromtimestamp(ts, tz)) def strftime(self, format): """ diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 9c9e11c6a4ba4..008731b13172e 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -292,13 +292,27 @@ def compare(x, y): compare(Timestamp.utcnow(), datetime.utcnow()) compare(Timestamp.today(), datetime.today()) current_time = calendar.timegm(datetime.now().utctimetuple()) + msg = "timezone-aware Timestamp with UTC" + with tm.assert_produces_warning(FutureWarning, match=msg): + # GH#22451 + ts_utc = Timestamp.utcfromtimestamp(current_time) compare( - Timestamp.utcfromtimestamp(current_time), + ts_utc, datetime.utcfromtimestamp(current_time), ) compare( Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time) ) + compare( + # Support tz kwarg in Timestamp.fromtimestamp + Timestamp.fromtimestamp(current_time, "UTC"), + datetime.fromtimestamp(current_time, utc), + ) + compare( + # Support tz kwarg in Timestamp.fromtimestamp + Timestamp.fromtimestamp(current_time, tz="UTC"), + datetime.fromtimestamp(current_time, utc), + ) date_component = datetime.utcnow() time_component = (date_component + timedelta(minutes=10)).time() @@ -322,8 +336,14 @@ def compare(x, y): compare(Timestamp.utcnow(), datetime.utcnow()) compare(Timestamp.today(), datetime.today()) current_time = calendar.timegm(datetime.now().utctimetuple()) + + msg = "timezone-aware Timestamp with UTC" + with tm.assert_produces_warning(FutureWarning, match=msg): + # GH#22451 + ts_utc = Timestamp.utcfromtimestamp(current_time) + compare( - Timestamp.utcfromtimestamp(current_time), + ts_utc, datetime.utcfromtimestamp(current_time), ) compare( From 906bc48a8227d683585344ff937f515c49de5db6 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 27 Dec 2021 16:15:53 -0800 Subject: [PATCH 2/3] whatsnew --- doc/source/whatsnew/v1.4.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index c743e38a118f7..fb0577da4f059 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -590,6 +590,7 @@ Other Deprecations - Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`) - Deprecated downcasting column-by-column in :meth:`DataFrame.where` with integer-dtypes (:issue:`44597`) - Deprecated ``numeric_only=None`` in :meth:`DataFrame.rank`; in a future version ``numeric_only`` must be either ``True`` or ``False`` (the default) (:issue:`45036`) +- Deprecated the behavior of :meth:`Timestamp.utcfromtimestamp`, in the future it will return a timezone-aware UTC :class:`Timestamp` (:issue:`22451`) - Deprecated :meth:`NaT.freq` (:issue:`45071`) - @@ -682,6 +683,7 @@ Datetimelike - Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`) - Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`) - Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`) +- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`??`) - Timedelta From 2214a613a0b49ce02ad5f93894b612db4b5d1153 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 27 Dec 2021 16:17:10 -0800 Subject: [PATCH 3/3] GH ref --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index fb0577da4f059..0b73575bf7aa6 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -683,7 +683,7 @@ Datetimelike - Bug in :meth:`Index.insert` for inserting ``np.datetime64``, ``np.timedelta64`` or ``tuple`` into :class:`Index` with ``dtype='object'`` with negative loc adding ``None`` and replacing existing value (:issue:`44509`) - Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`) - Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`) -- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`??`) +- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`45083`) - Timedelta