diff --git a/doc/source/whatsnew/v2.3.0.rst b/doc/source/whatsnew/v2.3.0.rst index 1f1b0c7d7195a..bba1ab68d9d05 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.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/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..1dae2403706e8 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1418,6 +1418,14 @@ 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. + # GH#56680 + "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 +1446,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. GH#56680 + "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/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_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 3975f3c46aaa1..f92e9145a2205 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,18 @@ def test_constructor_positional_keyword_mixed_with_tzinfo(self, kwd, request): 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) + def test_constructor_strptime(self): # GH#25016 # Test support for Timestamp.strptime 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) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index cb94427ae8961..e4e87a4b9c95e 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -1073,6 +1073,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", [