From 33a9eb1a1cdba8798ff3e2d2f6f4af4e9fd94922 Mon Sep 17 00:00:00 2001 From: Yian Shang Date: Wed, 28 Feb 2018 16:06:30 +0100 Subject: [PATCH 1/4] Making to_datetime('today') and Timestamp('today') consistent with one another --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/_libs/tslib.pyx | 4 ++-- pandas/tests/indexes/datetimes/test_tools.py | 19 +++++++++++++------ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index fb22dc40e335f..5cfaeaf335700 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -620,6 +620,7 @@ Other API Changes - Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`) - :class:`DateOffset` objects render more simply, e.g. ```` instead of ```` (:issue:`19403`) - ``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`) +- Making `to_datetime('today')` and `Timestamp('today')` consistent with one another (:issue:`19935`) .. _whatsnew_0230.deprecations: diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index fec7f21d6e6eb..b0387bf7516db 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -755,8 +755,8 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult): iresult[0] = Timestamp.utcnow().value return True elif val == 'today': - # Note: this is *not* the same as Timestamp('today') - iresult[0] = Timestamp.now().normalize().value + # Now this is consistent with Timestamp('today') + iresult[0] = Timestamp.today().value return True return False diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index fbf0977a04d82..0d42b6e9692fe 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -224,27 +224,34 @@ def test_to_datetime_today(self): # this both of these timezones _and_ UTC will all be in the same day, # so this test will not detect the regression introduced in #18666. with tm.set_timezone('Pacific/Auckland'): # 12-13 hours ahead of UTC - nptoday = np.datetime64('today').astype('datetime64[ns]') + nptoday = np.datetime64('today')\ + .astype('datetime64[ns]').astype(np.int64) pdtoday = pd.to_datetime('today') pdtoday2 = pd.to_datetime(['today'])[0] + tstoday = pd.Timestamp('today') + tstoday2 = pd.Timestamp.today() + # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds - assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 - assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 + assert abs(pdtoday.normalize().value - nptoday) < 1e10 + assert abs(pdtoday2.normalize().value - nptoday) < 1e10 + assert abs(pdtoday.value - tstoday.value) < 1e10 + assert abs(pdtoday.value - tstoday2.value) < 1e10 assert pdtoday.tzinfo is None assert pdtoday2.tzinfo is None with tm.set_timezone('US/Samoa'): # 11 hours behind UTC - nptoday = np.datetime64('today').astype('datetime64[ns]') + nptoday = np.datetime64('today')\ + .astype('datetime64[ns]').astype(np.int64) pdtoday = pd.to_datetime('today') pdtoday2 = pd.to_datetime(['today'])[0] # These should all be equal with infinite perf; this gives # a generous margin of 10 seconds - assert abs(pdtoday.value - nptoday.astype(np.int64)) < 1e10 - assert abs(pdtoday2.value - nptoday.astype(np.int64)) < 1e10 + assert abs(pdtoday.normalize().value - nptoday) < 1e10 + assert abs(pdtoday2.normalize().value - nptoday) < 1e10 assert pdtoday.tzinfo is None assert pdtoday2.tzinfo is None From 681bf531582fda18b7c1064b96752b1c6887b36c Mon Sep 17 00:00:00 2001 From: Yian Shang Date: Thu, 1 Mar 2018 14:13:11 +0100 Subject: [PATCH 2/4] minor changes based on review --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/_libs/tslib.pyx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index d44ab228b69ac..fbf8fcee3cd82 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -620,7 +620,7 @@ Other API Changes - Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`) - :class:`DateOffset` objects render more simply, e.g. ```` instead of ```` (:issue:`19403`) - ``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`) -- Making `to_datetime('today')` and `Timestamp('today')` consistent with one another (:issue:`19935`) +- `pd.to_datetime('today')` now returns the local datetime, which is consistent with `pd.Timestamp('today')`. `pd.to_datetime` previously returned a `normalized()` datetime (:issue:`19935`) - :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`) .. _whatsnew_0230.deprecations: diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index b0387bf7516db..17453d8af1297 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -755,7 +755,6 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult): iresult[0] = Timestamp.utcnow().value return True elif val == 'today': - # Now this is consistent with Timestamp('today') iresult[0] = Timestamp.today().value return True return False From e9e74170235d672192089bcf09e44125165c2b99 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Thu, 1 Mar 2018 17:59:36 -0500 Subject: [PATCH 3/4] doc --- doc/source/whatsnew/v0.23.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index fbf8fcee3cd82..9201d469e51b7 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -620,7 +620,7 @@ Other API Changes - Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`) - :class:`DateOffset` objects render more simply, e.g. ```` instead of ```` (:issue:`19403`) - ``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`) -- `pd.to_datetime('today')` now returns the local datetime, which is consistent with `pd.Timestamp('today')`. `pd.to_datetime` previously returned a `normalized()` datetime (:issue:`19935`) +- ``pd.to_datetime('today')`` now returns a datetime, now consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`) - :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`) .. _whatsnew_0230.deprecations: From 680000ec7a8820f758544a8f9635abaadd18614d Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Thu, 1 Mar 2018 18:02:11 -0500 Subject: [PATCH 4/4] doc --- doc/source/whatsnew/v0.23.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 530ef35ac494e..08363cd54c606 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -652,7 +652,7 @@ Other API Changes - Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`) - :class:`DateOffset` objects render more simply, e.g. ```` instead of ```` (:issue:`19403`) - ``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`) -- ``pd.to_datetime('today')`` now returns a datetime, now consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`) +- ``pd.to_datetime('today')`` now returns a datetime, consistent with ``pd.Timestamp('today')``; previously ``pd.to_datetime('today')`` returned a ``.normalized()`` datetime (:issue:`19935`) - :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`) .. _whatsnew_0230.deprecations: