Skip to content

BUG: Fix Timestamp('now') and Timestamp.now unit inconsistency #56281

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 8 commits into from
Dec 7, 2023
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bug fixes
~~~~~~~~~
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
- Bug in :class:`Series` when trying to cast date-like string inputs to :class:`ArrowDtype` of ``pyarrow.timestamp`` (:issue:`56266`)
- Bug in :class:`Timestamp` construction with ``ts_input="now"`` or ``ts_input="today"`` giving a different unit from :meth:`Timestamp.now` or :meth:`Timestamp.today` (:issue:`55879`)
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
- Fixed bug in :meth:`.DataFrameGroupBy.min` and :meth:`.DataFrameGroupBy.max` not preserving extension dtype for empty object (:issue:`55619`)
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,13 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
# Issue 9000, we short-circuit rather than going
# into np_datetime_strings which returns utc
dt = datetime.now(tz)
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
elif ts == "today":
# Issue 9000, we short-circuit rather than going
# into np_datetime_strings which returns a normalized datetime
dt = datetime.now(tz)
# equiv: datetime.today().replace(tzinfo=tz)
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
else:
string_to_dts_failed = string_to_dts(
ts, &dts, &out_bestunit, &out_local,
Expand Down Expand Up @@ -647,8 +649,6 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
reso = get_supported_reso(out_bestunit)
return convert_datetime_to_tsobject(dt, tz, nanos=nanos, reso=reso)

return convert_datetime_to_tsobject(dt, tz)


cdef check_overflows(_TSObject obj, NPY_DATETIMEUNIT reso=NPY_FR_ns):
"""
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ def test_constructor_str_infer_reso(self):
ts = Timestamp("2020-01-01 00+00:00")
assert ts.unit == "s"

@pytest.mark.parametrize("method", ["now", "today"])
def test_now_today_unit(self, method):
# GH#55879
ts_from_method = getattr(Timestamp, method)()
ts_from_string = Timestamp(method)
assert ts_from_method.unit == ts_from_string.unit == "us"


class TestTimestampConstructors:
def test_weekday_but_no_day_raises(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def test_to_datetime_now(self):
# See GH#18666
with tm.set_timezone("US/Eastern"):
# GH#18705
now = Timestamp("now")
now = Timestamp("now").as_unit("ns")
pdnow = to_datetime("now")
pdnow2 = to_datetime(["now"])[0]

Expand All @@ -1066,7 +1066,7 @@ def test_to_datetime_today(self, tz):
pdtoday = to_datetime("today")
pdtoday2 = to_datetime(["today"])[0]

tstoday = Timestamp("today")
tstoday = Timestamp("today").as_unit("ns")
tstoday2 = Timestamp.today().as_unit("ns")

# These should all be equal with infinite perf; this gives
Expand Down