Skip to content

BUG+DEPR: Timestamp.fromtimestamp tz arg #45083

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

Merged
merged 3 commits into from
Dec 28, 2021
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ Other Deprecations
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
- Deprecated downcasting column-by-column in :meth:`DataFrame.where` with integer-dtypes (:issue:`44597`)
- Deprecated ``numeric_only=None`` in :meth:`DataFrame.rank`; in a future version ``numeric_only`` must be either ``True`` or ``False`` (the default) (:issue:`45036`)
- Deprecated the behavior of :meth:`Timestamp.utcfromtimestamp`, in the future it will return a timezone-aware UTC :class:`Timestamp` (:issue:`22451`)
- Deprecated :meth:`NaT.freq` (:issue:`45071`)
-

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

Timedelta
Expand Down
15 changes: 13 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,20 @@ class Timestamp(_Timestamp):
>>> pd.Timestamp.utcfromtimestamp(1584199972)
Timestamp('2020-03-14 15:32:52')
"""
# GH#22451
warnings.warn(
"The behavior of Timestamp.utcfromtimestamp is deprecated, in a "
"future version will return a timezone-aware Timestamp with UTC "
"timezone. To keep the old behavior, use "
"Timestamp.utcfromtimestamp(ts).tz_localize(None). "
"To get the future behavior, use Timestamp.fromtimestamp(ts, 'UTC')",
FutureWarning,
stacklevel=1,
)
return cls(datetime.utcfromtimestamp(ts))

@classmethod
def fromtimestamp(cls, ts):
def fromtimestamp(cls, ts, tz=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type generally if possible (followup ok)

"""
Timestamp.fromtimestamp(ts)

Expand All @@ -1180,7 +1190,8 @@ class Timestamp(_Timestamp):

Note that the output may change depending on your local time.
"""
return cls(datetime.fromtimestamp(ts))
tz = maybe_get_tz(tz)
return cls(datetime.fromtimestamp(ts, tz))

def strftime(self, format):
"""
Expand Down
24 changes: 22 additions & 2 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,27 @@ def compare(x, y):
compare(Timestamp.utcnow(), datetime.utcnow())
compare(Timestamp.today(), datetime.today())
current_time = calendar.timegm(datetime.now().utctimetuple())
msg = "timezone-aware Timestamp with UTC"
with tm.assert_produces_warning(FutureWarning, match=msg):
# GH#22451
ts_utc = Timestamp.utcfromtimestamp(current_time)
compare(
Timestamp.utcfromtimestamp(current_time),
ts_utc,
datetime.utcfromtimestamp(current_time),
)
compare(
Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time)
)
compare(
# Support tz kwarg in Timestamp.fromtimestamp
Timestamp.fromtimestamp(current_time, "UTC"),
datetime.fromtimestamp(current_time, utc),
)
compare(
# Support tz kwarg in Timestamp.fromtimestamp
Timestamp.fromtimestamp(current_time, tz="UTC"),
datetime.fromtimestamp(current_time, utc),
)

date_component = datetime.utcnow()
time_component = (date_component + timedelta(minutes=10)).time()
Expand All @@ -322,8 +336,14 @@ def compare(x, y):
compare(Timestamp.utcnow(), datetime.utcnow())
compare(Timestamp.today(), datetime.today())
current_time = calendar.timegm(datetime.now().utctimetuple())

msg = "timezone-aware Timestamp with UTC"
with tm.assert_produces_warning(FutureWarning, match=msg):
# GH#22451
ts_utc = Timestamp.utcfromtimestamp(current_time)

compare(
Timestamp.utcfromtimestamp(current_time),
ts_utc,
datetime.utcfromtimestamp(current_time),
)
compare(
Expand Down