Skip to content

Commit d6fdbee

Browse files
committed
BUG: Fix Timestamp('now') and Timestamp.now unit inconsistency
1 parent f7c73a5 commit d6fdbee

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

doc/source/whatsnew/v2.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
Bug fixes
2323
~~~~~~~~~
2424
- Bug in :class:`Series` constructor raising DeprecationWarning when ``index`` is a list of :class:`Series` (:issue:`55228`)
25+
- 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`)
2526
- Bug in :meth:`Index.__getitem__` returning wrong result for Arrow dtypes and negative stepsize (:issue:`55832`)
2627
- Fixed bug in :func:`to_numeric` converting to extension dtype for ``string[pyarrow_numpy]`` dtype (:issue:`56179`)
2728
- Fixed bug in :meth:`DataFrame.__setitem__` casting :class:`Index` with object-dtype to PyArrow backed strings when ``infer_string`` option is set (:issue:`55638`)

pandas/_libs/tslibs/conversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ cdef _TSObject convert_str_to_tsobject(str ts, tzinfo tz,
646646
reso = get_supported_reso(out_bestunit)
647647
return convert_datetime_to_tsobject(dt, tz, nanos=nanos, reso=reso)
648648

649-
return convert_datetime_to_tsobject(dt, tz)
649+
return convert_datetime_to_tsobject(dt, tz, nanos=0, reso=NPY_FR_us)
650650

651651

652652
cdef check_overflows(_TSObject obj, NPY_DATETIMEUNIT reso=NPY_FR_ns):

pandas/tests/scalar/timestamp/test_constructors.py

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

467+
def test_now_today_unit(self):
468+
# GH#55879
469+
ts_now_from_method = Timestamp.now()
470+
ts_now_from_string = Timestamp("now")
471+
ts_today_from_method = Timestamp.today()
472+
ts_today_from_string = Timestamp("today")
473+
assert (
474+
ts_now_from_method.unit
475+
== ts_now_from_string.unit
476+
== ts_today_from_method.unit
477+
== ts_today_from_string.unit
478+
== "us"
479+
)
480+
467481

468482
class TestTimestampConstructors:
469483
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)