Skip to content

Commit 9f51d4f

Browse files
yuanx749mroeschke
andauthored
BUG: Fix Timestamp('now') and Timestamp.now unit inconsistency (#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]>
1 parent 2fc264a commit 9f51d4f

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

doc/source/whatsnew/v2.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bug fixes
2323
~~~~~~~~~
2424
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
2525
- Bug in :class:`Series` when trying to cast date-like string inputs to :class:`ArrowDtype` of ``pyarrow.timestamp`` (:issue:`56266`)
26+
- 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`)
2627
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
2728
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
2829
- Fixed bug in :meth:`.DataFrameGroupBy.min` and :meth:`.DataFrameGroupBy.max` not preserving extension dtype for empty object (:issue:`55619`)

pandas/_libs/tslibs/conversion.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,13 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
599599
# Issue 9000, we short-circuit rather than going
600600
# into np_datetime_strings which returns utc
601601
dt = datetime.now(tz)
602+
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
602603
elif ts == "today":
603604
# Issue 9000, we short-circuit rather than going
604605
# into np_datetime_strings which returns a normalized datetime
605606
dt = datetime.now(tz)
606607
# equiv: datetime.today().replace(tzinfo=tz)
608+
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
607609
else:
608610
string_to_dts_failed = string_to_dts(
609611
ts, &dts, &out_bestunit, &out_local,
@@ -647,8 +649,6 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
647649
reso = get_supported_reso(out_bestunit)
648650
return convert_datetime_to_tsobject(dt, tz, nanos=nanos, reso=reso)
649651

650-
return convert_datetime_to_tsobject(dt, tz)
651-
652652

653653
cdef check_overflows(_TSObject obj, NPY_DATETIMEUNIT reso=NPY_FR_ns):
654654
"""

pandas/tests/scalar/timestamp/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ def test_constructor_str_infer_reso(self):
464464
ts = Timestamp("2020-01-01 00+00:00")
465465
assert ts.unit == "s"
466466

467+
@pytest.mark.parametrize("method", ["now", "today"])
468+
def test_now_today_unit(self, method):
469+
# GH#55879
470+
ts_from_method = getattr(Timestamp, method)()
471+
ts_from_string = Timestamp(method)
472+
assert ts_from_method.unit == ts_from_string.unit == "us"
473+
467474

468475
class TestTimestampConstructors:
469476
def test_weekday_but_no_day_raises(self):

pandas/tests/tools/test_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ def test_to_datetime_now(self):
10401040
# See GH#18666
10411041
with tm.set_timezone("US/Eastern"):
10421042
# GH#18705
1043-
now = Timestamp("now")
1043+
now = Timestamp("now").as_unit("ns")
10441044
pdnow = to_datetime("now")
10451045
pdnow2 = to_datetime(["now"])[0]
10461046

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

1069-
tstoday = Timestamp("today")
1069+
tstoday = Timestamp("today").as_unit("ns")
10701070
tstoday2 = Timestamp.today().as_unit("ns")
10711071

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

0 commit comments

Comments
 (0)