Skip to content

DEPR: to_datetime('now') match Timestamp('now') #49346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ Removal of prior version deprecations/changes
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)
- Removed the deprecated method ``tshift`` from pandas classes (:issue:`11631`)
- Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`)
- Changed behavior of :class:`DataFrame` constructor given floating-point ``data`` and an integer ``dtype``, when the data cannot be cast losslessly, the floating point dtype is retained, matching :class:`Series` behavior (:issue:`41170`)
- Changed behavior of :class:`DataFrame` constructor when passed a ``dtype`` (other than int) that the data cannot be cast to; it now raises instead of silently ignoring the dtype (:issue:`41733`)
- Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`)
Expand Down
18 changes: 4 additions & 14 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import warnings

cimport cython
from cpython.datetime cimport (
PyDate_Check,
Expand All @@ -9,8 +7,6 @@ from cpython.datetime cimport (
tzinfo,
)

from pandas.util._exceptions import find_stack_level

# import datetime C API
import_datetime()

Expand Down Expand Up @@ -855,17 +851,11 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
# We delay this check for as long as possible
# because it catches relatively rare cases
if val == "now":
iresult[0] = Timestamp.utcnow().value
if not utc:
if utc:
iresult[0] = Timestamp.utcnow().value
else:
# GH#18705 make sure to_datetime("now") matches Timestamp("now")
warnings.warn(
"The parsing of 'now' in pd.to_datetime without `utc=True` is "
"deprecated. In a future version, this will match Timestamp('now') "
"and Timestamp.now()",
FutureWarning,
stacklevel=find_stack_level(),
)

iresult[0] = Timestamp("now").value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a bit more performant to use Timestamp.now instead

In [3]: %timeit Timestamp.now()
2.04 µs ± 32.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [4]: %timeit Timestamp("now")
2.25 µs ± 29.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like np.datetime64("now", "ns").view("i8") is 2x faster than Timestamp.now().value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out Timestamp.now() was the way to go. updated.

return True
elif val == "today":
iresult[0] = Timestamp.today().value
Expand Down
24 changes: 7 additions & 17 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,15 @@ def test_to_datetime_unparsable_ignore(self):
def test_to_datetime_now(self):
# See GH#18666
with tm.set_timezone("US/Eastern"):
msg = "The parsing of 'now' in pd.to_datetime"
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
# checking stacklevel is tricky because we go through cython code
# GH#18705
npnow = np.datetime64("now").astype("datetime64[ns]")
pdnow = to_datetime("now")
pdnow2 = to_datetime(["now"])[0]
# GH#18705
now = Timestamp("now")
pdnow = to_datetime("now")
pdnow2 = to_datetime(["now"])[0]

# These should all be equal with infinite perf; this gives
# a generous margin of 10 seconds
assert abs(pdnow.value - npnow.astype(np.int64)) < 1e10
assert abs(pdnow2.value - npnow.astype(np.int64)) < 1e10
assert abs(pdnow.value - now.value) < 1e10
assert abs(pdnow2.value - now.value) < 1e10

assert pdnow.tzinfo is None
assert pdnow2.tzinfo is None
Expand Down Expand Up @@ -673,12 +668,7 @@ def test_to_datetime_today(self, tz):

@pytest.mark.parametrize("arg", ["now", "today"])
def test_to_datetime_today_now_unicode_bytes(self, arg):
warn = FutureWarning if arg == "now" else None
msg = "The parsing of 'now' in pd.to_datetime"
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
# checking stacklevel is tricky because we go through cython code
# GH#18705
to_datetime([arg])
to_datetime([arg])

@pytest.mark.parametrize(
"dt", [np.datetime64("2000-01-01"), np.datetime64("2000-01-02")]
Expand Down