Skip to content

Commit

Permalink
BUG: Fix Timestamp('now') and Timestamp.now unit inconsistency (#…
Browse files Browse the repository at this point in the history
…56281)

* BUG: Fix `Timestamp('now')` and `Timestamp.now` unit inconsistency

* Move return

* Update doc/source/whatsnew/v2.1.4.rst

Co-authored-by: Matthew Roeschke <[email protected]>

* Resolve

* not use tuple in cython for perf

---------

Co-authored-by: Matthew Roeschke <[email protected]>
  • Loading branch information
yuanx749 and mroeschke authored Dec 7, 2023
1 parent 2fc264a commit 9f51d4f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
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

0 comments on commit 9f51d4f

Please sign in to comment.