Skip to content

Commit 764b1ef

Browse files
authored
DEPR: utcnow, utcfromtimestamp (#56680)
* DEPR: utcnow, utcfromtimestamp * GH ref * update tests
1 parent e619d08 commit 764b1ef

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ Other API changes
9292

9393
Deprecations
9494
~~~~~~~~~~~~
95-
-
95+
- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`)
96+
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
9697
-
9798

9899
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/strptime.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ cdef bint parse_today_now(
132132
if infer_reso:
133133
creso = NPY_DATETIMEUNIT.NPY_FR_us
134134
if utc:
135-
ts = <_Timestamp>Timestamp.utcnow()
135+
ts = <_Timestamp>Timestamp.now(timezone.utc)
136136
iresult[0] = ts._as_creso(creso)._value
137137
else:
138138
# GH#18705 make sure to_datetime("now") matches Timestamp("now")

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,14 @@ class Timestamp(_Timestamp):
14181418
>>> pd.Timestamp.utcnow() # doctest: +SKIP
14191419
Timestamp('2020-11-16 22:50:18.092888+0000', tz='UTC')
14201420
"""
1421+
warnings.warn(
1422+
# The stdlib datetime.utcnow is deprecated, so we deprecate to match.
1423+
# GH#56680
1424+
"Timestamp.utcnow is deprecated and will be removed in a future "
1425+
"version. Use Timestamp.now('UTC') instead.",
1426+
FutureWarning,
1427+
stacklevel=find_stack_level(),
1428+
)
14211429
return cls.now(UTC)
14221430

14231431
@classmethod
@@ -1438,6 +1446,14 @@ class Timestamp(_Timestamp):
14381446
Timestamp('2020-03-14 15:32:52+0000', tz='UTC')
14391447
"""
14401448
# GH#22451
1449+
warnings.warn(
1450+
# The stdlib datetime.utcfromtimestamp is deprecated, so we deprecate
1451+
# to match. GH#56680
1452+
"Timestamp.utcfromtimestamp is deprecated and will be removed in a "
1453+
"future version. Use Timestamp.fromtimestamp(ts, 'UTC') instead.",
1454+
FutureWarning,
1455+
stacklevel=find_stack_level(),
1456+
)
14411457
return cls.fromtimestamp(ts, tz="UTC")
14421458

14431459
@classmethod

pandas/tests/groupby/methods/test_groupby_shift_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_group_shift_with_fill_value():
6363

6464
def test_group_shift_lose_timezone():
6565
# GH 30134
66-
now_dt = Timestamp.utcnow().as_unit("ns")
66+
now_dt = Timestamp.now("UTC").as_unit("ns")
6767
df = DataFrame({"a": [1, 1], "date": now_dt})
6868
result = df.groupby("a").shift(0).iloc[0]
6969
expected = Series({"date": now_dt}, name=result.name)

pandas/tests/scalar/timestamp/test_constructors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Timedelta,
2929
Timestamp,
3030
)
31+
import pandas._testing as tm
3132

3233

3334
class TestTimestampConstructorUnitKeyword:
@@ -329,6 +330,18 @@ def test_constructor_positional_keyword_mixed_with_tzinfo(self, kwd, request):
329330
class TestTimestampClassMethodConstructors:
330331
# Timestamp constructors other than __new__
331332

333+
def test_utcnow_deprecated(self):
334+
# GH#56680
335+
msg = "Timestamp.utcnow is deprecated"
336+
with tm.assert_produces_warning(FutureWarning, match=msg):
337+
Timestamp.utcnow()
338+
339+
def test_utcfromtimestamp_deprecated(self):
340+
# GH#56680
341+
msg = "Timestamp.utcfromtimestamp is deprecated"
342+
with tm.assert_produces_warning(FutureWarning, match=msg):
343+
Timestamp.utcfromtimestamp(43)
344+
332345
def test_constructor_strptime(self):
333346
# GH#25016
334347
# Test support for Timestamp.strptime

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@ def test_disallow_setting_tz(self, tz):
270270
ts.tz = tz
271271

272272
def test_default_to_stdlib_utc(self):
273-
assert Timestamp.utcnow().tz is timezone.utc
273+
msg = "Timestamp.utcnow is deprecated"
274+
with tm.assert_produces_warning(FutureWarning, match=msg):
275+
assert Timestamp.utcnow().tz is timezone.utc
274276
assert Timestamp.now("UTC").tz is timezone.utc
275277
assert Timestamp("2016-01-01", tz="UTC").tz is timezone.utc
276278

@@ -313,11 +315,15 @@ def compare(x, y):
313315
compare(Timestamp.now(), datetime.now())
314316
compare(Timestamp.now("UTC"), datetime.now(pytz.timezone("UTC")))
315317
compare(Timestamp.now("UTC"), datetime.now(tzutc()))
316-
compare(Timestamp.utcnow(), datetime.now(timezone.utc))
318+
msg = "Timestamp.utcnow is deprecated"
319+
with tm.assert_produces_warning(FutureWarning, match=msg):
320+
compare(Timestamp.utcnow(), datetime.now(timezone.utc))
317321
compare(Timestamp.today(), datetime.today())
318322
current_time = calendar.timegm(datetime.now().utctimetuple())
319323

320-
ts_utc = Timestamp.utcfromtimestamp(current_time)
324+
msg = "Timestamp.utcfromtimestamp is deprecated"
325+
with tm.assert_produces_warning(FutureWarning, match=msg):
326+
ts_utc = Timestamp.utcfromtimestamp(current_time)
321327
assert ts_utc.timestamp() == current_time
322328
compare(
323329
Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time)

pandas/tests/tools/test_to_datetime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ def test_to_datetime_today(self, tz):
10721072
def test_to_datetime_today_now_unicode_bytes(self, arg):
10731073
to_datetime([arg])
10741074

1075+
@pytest.mark.filterwarnings("ignore:Timestamp.utcnow is deprecated:FutureWarning")
10751076
@pytest.mark.parametrize(
10761077
"format, expected_ds",
10771078
[

0 commit comments

Comments
 (0)