From ea3f6c76756e34981a6909ee69dc0db30b0e76bb Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 29 Dec 2023 09:47:28 -0800 Subject: [PATCH 1/3] DEPR: utcnow, utcfromtimestamp --- doc/source/whatsnew/v2.3.0.rst | 3 ++- pandas/_libs/tslibs/strptime.pyx | 2 +- pandas/_libs/tslibs/timestamps.pyx | 15 +++++++++++++++ .../tests/scalar/timestamp/test_constructors.py | 11 +++++++++++ pandas/tests/tools/test_to_datetime.py | 1 + 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 1f1b0c7d7195a..5aa563b65d061 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -92,7 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- +- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`??`) +- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`??`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index ee72b1311051e..c09835c9661f3 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -132,7 +132,7 @@ cdef bint parse_today_now( if infer_reso: creso = NPY_DATETIMEUNIT.NPY_FR_us if utc: - ts = <_Timestamp>Timestamp.utcnow() + ts = <_Timestamp>Timestamp.now(timezone.utc) iresult[0] = ts._as_creso(creso)._value else: # GH#18705 make sure to_datetime("now") matches Timestamp("now") diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 568539b53aee0..e8b59cba4b8ab 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1418,6 +1418,13 @@ class Timestamp(_Timestamp): >>> pd.Timestamp.utcnow() # doctest: +SKIP Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC') """ + warnings.warn( + # The stdlib datetime.utcnow is deprecated, so we deprecate to match. + "Timestamp.utcnow is deprecated and will be removed in a future " + "version. Use Timestamp.now('UTC') instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) return cls.now(UTC) @classmethod @@ -1438,6 +1445,14 @@ class Timestamp(_Timestamp): Timestamp('2020-03-14 15:32:52+0000', tz='UTC') """ # GH#22451 + warnings.warn( + # The stdlib datetime.utcfromtimestamp is deprecated, so we deprecate + # to match. + "Timestamp.utcfromtimestamp is deprecated and will be removed in a " + "future version. Use Timestamp.fromtimestamp(ts, 'UTC') instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) return cls.fromtimestamp(ts, tz="UTC") @classmethod diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 3975f3c46aaa1..abe6f353af2e5 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -28,6 +28,7 @@ Timedelta, Timestamp, ) +import pandas._testing as tm class TestTimestampConstructorUnitKeyword: @@ -329,6 +330,16 @@ def test_constructor_positional_keyword_mixed_with_tzinfo(self, kwd, request): class TestTimestampClassMethodConstructors: # Timestamp constructors other than __new__ + def test_utcnow_deprecated(self): + msg = "Timestamp.utcnow is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + Timestamp.utcnow() + + def test_utcfromtimestamp_deprecated(self): + msg = "Timestamp.utcfromtimestamp is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + Timestamp.utcfromtimestamp(43) + def test_constructor_strptime(self): # GH#25016 # Test support for Timestamp.strptime diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 6791ac0340640..b7a4099fbdcb1 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -1083,6 +1083,7 @@ def test_to_datetime_today(self, tz): def test_to_datetime_today_now_unicode_bytes(self, arg): to_datetime([arg]) + @pytest.mark.filterwarnings("ignore:Timestamp.utcnow is deprecated:FutureWarning") @pytest.mark.parametrize( "format, expected_ds", [ From c989d7118c05a25b0b596ed53de2e94ce8cf6584 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 29 Dec 2023 09:48:48 -0800 Subject: [PATCH 2/3] GH ref --- doc/source/whatsnew/v2.3.0.rst | 4 ++-- pandas/_libs/tslibs/timestamps.pyx | 3 ++- pandas/tests/scalar/timestamp/test_constructors.py | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 5aa563b65d061..bba1ab68d9d05 100644 --- a/doc/source/whatsnew/v2.3.0.rst +++ b/doc/source/whatsnew/v2.3.0.rst @@ -92,8 +92,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`??`) -- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`??`) +- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) +- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) - .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index e8b59cba4b8ab..1dae2403706e8 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1420,6 +1420,7 @@ class Timestamp(_Timestamp): """ warnings.warn( # The stdlib datetime.utcnow is deprecated, so we deprecate to match. + # GH#56680 "Timestamp.utcnow is deprecated and will be removed in a future " "version. Use Timestamp.now('UTC') instead.", FutureWarning, @@ -1447,7 +1448,7 @@ class Timestamp(_Timestamp): # GH#22451 warnings.warn( # The stdlib datetime.utcfromtimestamp is deprecated, so we deprecate - # to match. + # to match. GH#56680 "Timestamp.utcfromtimestamp is deprecated and will be removed in a " "future version. Use Timestamp.fromtimestamp(ts, 'UTC') instead.", FutureWarning, diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index abe6f353af2e5..f92e9145a2205 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -331,11 +331,13 @@ class TestTimestampClassMethodConstructors: # Timestamp constructors other than __new__ def test_utcnow_deprecated(self): + # GH#56680 msg = "Timestamp.utcnow is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): Timestamp.utcnow() def test_utcfromtimestamp_deprecated(self): + # GH#56680 msg = "Timestamp.utcfromtimestamp is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): Timestamp.utcfromtimestamp(43) From b073fd7fc8bf46d87c3d06da3eaaebe30a9bf061 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 3 Jan 2024 10:50:35 -0800 Subject: [PATCH 3/3] update tests --- .../tests/groupby/methods/test_groupby_shift_diff.py | 2 +- pandas/tests/scalar/timestamp/test_timestamp.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pandas/tests/groupby/methods/test_groupby_shift_diff.py b/pandas/tests/groupby/methods/test_groupby_shift_diff.py index 94e672d4892fe..41e0ee93a5941 100644 --- a/pandas/tests/groupby/methods/test_groupby_shift_diff.py +++ b/pandas/tests/groupby/methods/test_groupby_shift_diff.py @@ -63,7 +63,7 @@ def test_group_shift_with_fill_value(): def test_group_shift_lose_timezone(): # GH 30134 - now_dt = Timestamp.utcnow().as_unit("ns") + now_dt = Timestamp.now("UTC").as_unit("ns") df = DataFrame({"a": [1, 1], "date": now_dt}) result = df.groupby("a").shift(0).iloc[0] expected = Series({"date": now_dt}, name=result.name) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 05e1c93e1a676..e0734b314a0bd 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -269,7 +269,9 @@ def test_disallow_setting_tz(self, tz): ts.tz = tz def test_default_to_stdlib_utc(self): - assert Timestamp.utcnow().tz is timezone.utc + msg = "Timestamp.utcnow is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + assert Timestamp.utcnow().tz is timezone.utc assert Timestamp.now("UTC").tz is timezone.utc assert Timestamp("2016-01-01", tz="UTC").tz is timezone.utc @@ -312,11 +314,15 @@ def compare(x, y): compare(Timestamp.now(), datetime.now()) compare(Timestamp.now("UTC"), datetime.now(pytz.timezone("UTC"))) compare(Timestamp.now("UTC"), datetime.now(tzutc())) - compare(Timestamp.utcnow(), datetime.now(timezone.utc)) + msg = "Timestamp.utcnow is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + compare(Timestamp.utcnow(), datetime.now(timezone.utc)) compare(Timestamp.today(), datetime.today()) current_time = calendar.timegm(datetime.now().utctimetuple()) - ts_utc = Timestamp.utcfromtimestamp(current_time) + msg = "Timestamp.utcfromtimestamp is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + ts_utc = Timestamp.utcfromtimestamp(current_time) assert ts_utc.timestamp() == current_time compare( Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time)