Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: utcnow, utcfromtimestamp #56680

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/methods/test_groupby_shift_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
Timedelta,
Timestamp,
)
import pandas._testing as tm


class TestTimestampConstructorUnitKeyword:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
Loading