Skip to content

Commit 358eaec

Browse files
authored
BUG+DEPR: Timestamp.fromtimestamp tz arg (#45083)
1 parent ec0c06b commit 358eaec

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ Other Deprecations
609609
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
610610
- Deprecated downcasting column-by-column in :meth:`DataFrame.where` with integer-dtypes (:issue:`44597`)
611611
- Deprecated ``numeric_only=None`` in :meth:`DataFrame.rank`; in a future version ``numeric_only`` must be either ``True`` or ``False`` (the default) (:issue:`45036`)
612+
- Deprecated the behavior of :meth:`Timestamp.utcfromtimestamp`, in the future it will return a timezone-aware UTC :class:`Timestamp` (:issue:`22451`)
612613
- Deprecated :meth:`NaT.freq` (:issue:`45071`)
613614
-
614615

@@ -701,6 +702,7 @@ Datetimelike
701702
- 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`)
702703
- Bug in :meth:`Series.mode` with ``DatetimeTZDtype`` incorrectly returning timezone-naive and ``PeriodDtype`` incorrectly raising (:issue:`41927`)
703704
- Bug in :class:`DateOffset`` addition with :class:`Timestamp` where ``offset.nanoseconds`` would not be included in the result (:issue:`43968`, :issue:`36589`)
705+
- Bug in :meth:`Timestamp.fromtimestamp` not supporting the ``tz`` argument (:issue:`45083`)
704706
-
705707

706708
Timedelta

pandas/_libs/tslibs/timestamps.pyx

+13-2
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,20 @@ class Timestamp(_Timestamp):
11641164
>>> pd.Timestamp.utcfromtimestamp(1584199972)
11651165
Timestamp('2020-03-14 15:32:52')
11661166
"""
1167+
# GH#22451
1168+
warnings.warn(
1169+
"The behavior of Timestamp.utcfromtimestamp is deprecated, in a "
1170+
"future version will return a timezone-aware Timestamp with UTC "
1171+
"timezone. To keep the old behavior, use "
1172+
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
1173+
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
1174+
FutureWarning,
1175+
stacklevel=1,
1176+
)
11671177
return cls(datetime.utcfromtimestamp(ts))
11681178

11691179
@classmethod
1170-
def fromtimestamp(cls, ts):
1180+
def fromtimestamp(cls, ts, tz=None):
11711181
"""
11721182
Timestamp.fromtimestamp(ts)
11731183
@@ -1180,7 +1190,8 @@ class Timestamp(_Timestamp):
11801190
11811191
Note that the output may change depending on your local time.
11821192
"""
1183-
return cls(datetime.fromtimestamp(ts))
1193+
tz = maybe_get_tz(tz)
1194+
return cls(datetime.fromtimestamp(ts, tz))
11841195

11851196
def strftime(self, format):
11861197
"""

pandas/tests/scalar/timestamp/test_timestamp.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,27 @@ def compare(x, y):
292292
compare(Timestamp.utcnow(), datetime.utcnow())
293293
compare(Timestamp.today(), datetime.today())
294294
current_time = calendar.timegm(datetime.now().utctimetuple())
295+
msg = "timezone-aware Timestamp with UTC"
296+
with tm.assert_produces_warning(FutureWarning, match=msg):
297+
# GH#22451
298+
ts_utc = Timestamp.utcfromtimestamp(current_time)
295299
compare(
296-
Timestamp.utcfromtimestamp(current_time),
300+
ts_utc,
297301
datetime.utcfromtimestamp(current_time),
298302
)
299303
compare(
300304
Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time)
301305
)
306+
compare(
307+
# Support tz kwarg in Timestamp.fromtimestamp
308+
Timestamp.fromtimestamp(current_time, "UTC"),
309+
datetime.fromtimestamp(current_time, utc),
310+
)
311+
compare(
312+
# Support tz kwarg in Timestamp.fromtimestamp
313+
Timestamp.fromtimestamp(current_time, tz="UTC"),
314+
datetime.fromtimestamp(current_time, utc),
315+
)
302316

303317
date_component = datetime.utcnow()
304318
time_component = (date_component + timedelta(minutes=10)).time()
@@ -322,8 +336,14 @@ def compare(x, y):
322336
compare(Timestamp.utcnow(), datetime.utcnow())
323337
compare(Timestamp.today(), datetime.today())
324338
current_time = calendar.timegm(datetime.now().utctimetuple())
339+
340+
msg = "timezone-aware Timestamp with UTC"
341+
with tm.assert_produces_warning(FutureWarning, match=msg):
342+
# GH#22451
343+
ts_utc = Timestamp.utcfromtimestamp(current_time)
344+
325345
compare(
326-
Timestamp.utcfromtimestamp(current_time),
346+
ts_utc,
327347
datetime.utcfromtimestamp(current_time),
328348
)
329349
compare(

0 commit comments

Comments
 (0)