Skip to content

Commit d44a6ec

Browse files
shangyianjreback
authored andcommitted
Making to_datetime('today') and Timestamp('today') consistent (#19937)
1 parent 87fefe2 commit d44a6ec

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ Other API Changes
652652
- Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`)
653653
- :class:`DateOffset` objects render more simply, e.g. ``<DateOffset: days=1>`` instead of ``<DateOffset: kwds={'days': 1}>`` (:issue:`19403`)
654654
- ``Categorical.fillna`` now validates its ``value`` and ``method`` keyword arguments. It now raises when both or none are specified, matching the behavior of :meth:`Series.fillna` (:issue:`19682`)
655+
- ``pd.to_datetime('today')`` now returns a datetime, consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`)
655656
- :func:`Series.str.replace` now takes an optional `regex` keyword which, when set to ``False``, uses literal string replacement rather than regex replacement (:issue:`16808`)
656657

657658
.. _whatsnew_0230.deprecations:

pandas/_libs/tslib.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,7 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult):
755755
iresult[0] = Timestamp.utcnow().value
756756
return True
757757
elif val == 'today':
758-
# Note: this is *not* the same as Timestamp('today')
759-
iresult[0] = Timestamp.now().normalize().value
758+
iresult[0] = Timestamp.today().value
760759
return True
761760
return False
762761

pandas/tests/indexes/datetimes/test_tools.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,34 @@ def test_to_datetime_today(self):
224224
# this both of these timezones _and_ UTC will all be in the same day,
225225
# so this test will not detect the regression introduced in #18666.
226226
with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC
227-
nptoday = np.datetime64('today').astype('datetime64[ns]')
227+
nptoday = np.datetime64('today')\
228+
.astype('datetime64[ns]').astype(np.int64)
228229
pdtoday = pd.to_datetime('today')
229230
pdtoday2 = pd.to_datetime(['today'])[0]
230231

232+
tstoday = pd.Timestamp('today')
233+
tstoday2 = pd.Timestamp.today()
234+
231235
# These should all be equal with infinite perf; this gives
232236
# a generous margin of 10 seconds
233-
assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10
234-
assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10
237+
assert abs(pdtoday.normalize().value - nptoday) < 1e10
238+
assert abs(pdtoday2.normalize().value - nptoday) < 1e10
239+
assert abs(pdtoday.value - tstoday.value) < 1e10
240+
assert abs(pdtoday.value - tstoday2.value) < 1e10
235241

236242
assert pdtoday.tzinfo is None
237243
assert pdtoday2.tzinfo is None
238244

239245
with tm.set_timezone('US/Samoa'): # 11 hours behind UTC
240-
nptoday = np.datetime64('today').astype('datetime64[ns]')
246+
nptoday = np.datetime64('today')\
247+
.astype('datetime64[ns]').astype(np.int64)
241248
pdtoday = pd.to_datetime('today')
242249
pdtoday2 = pd.to_datetime(['today'])[0]
243250

244251
# These should all be equal with infinite perf; this gives
245252
# a generous margin of 10 seconds
246-
assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10
247-
assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10
253+
assert abs(pdtoday.normalize().value - nptoday) < 1e10
254+
assert abs(pdtoday2.normalize().value - nptoday) < 1e10
248255

249256
assert pdtoday.tzinfo is None
250257
assert pdtoday2.tzinfo is None

0 commit comments

Comments
 (0)