Skip to content

Commit 8564b70

Browse files
authored
DEPR: to_datetime('now') match Timestamp('now') (#49346)
* DEPR: to_datetime('now') match Timestamp('now') * perf * try again
1 parent 30589f7 commit 8564b70

File tree

3 files changed

+13
-31
lines changed

3 files changed

+13
-31
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ Removal of prior version deprecations/changes
253253
- Removed the ``display.column_space`` option in favor of ``df.to_string(col_space=...)`` (:issue:`47280`)
254254
- Removed the deprecated method ``mad`` from pandas classes (:issue:`11787`)
255255
- Removed the deprecated method ``tshift`` from pandas classes (:issue:`11631`)
256+
- Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`)
256257
- 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`)
257258
- 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`)
258259
- Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`)

pandas/_libs/tslib.pyx

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import warnings
2-
31
cimport cython
42
from cpython.datetime cimport (
53
PyDate_Check,
@@ -9,8 +7,6 @@ from cpython.datetime cimport (
97
tzinfo,
108
)
119

12-
from pandas.util._exceptions import find_stack_level
13-
1410
# import datetime C API
1511
import_datetime()
1612

@@ -855,17 +851,12 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult, bint utc):
855851
# We delay this check for as long as possible
856852
# because it catches relatively rare cases
857853
if val == "now":
858-
iresult[0] = Timestamp.utcnow().value
859-
if not utc:
854+
if utc:
855+
iresult[0] = Timestamp.utcnow().value
856+
else:
860857
# GH#18705 make sure to_datetime("now") matches Timestamp("now")
861-
warnings.warn(
862-
"The parsing of 'now' in pd.to_datetime without `utc=True` is "
863-
"deprecated. In a future version, this will match Timestamp('now') "
864-
"and Timestamp.now()",
865-
FutureWarning,
866-
stacklevel=find_stack_level(),
867-
)
868-
858+
# Note using Timestamp.now() is faster than Timestamp("now")
859+
iresult[0] = Timestamp.now().value
869860
return True
870861
elif val == "today":
871862
iresult[0] = Timestamp.today().value

pandas/tests/tools/test_to_datetime.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,15 @@ def test_to_datetime_unparsable_ignore(self):
626626
def test_to_datetime_now(self):
627627
# See GH#18666
628628
with tm.set_timezone("US/Eastern"):
629-
msg = "The parsing of 'now' in pd.to_datetime"
630-
with tm.assert_produces_warning(
631-
FutureWarning, match=msg, check_stacklevel=False
632-
):
633-
# checking stacklevel is tricky because we go through cython code
634-
# GH#18705
635-
npnow = np.datetime64("now").astype("datetime64[ns]")
636-
pdnow = to_datetime("now")
637-
pdnow2 = to_datetime(["now"])[0]
629+
# GH#18705
630+
now = Timestamp("now")
631+
pdnow = to_datetime("now")
632+
pdnow2 = to_datetime(["now"])[0]
638633

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

644639
assert pdnow.tzinfo is None
645640
assert pdnow2.tzinfo is None
@@ -673,12 +668,7 @@ def test_to_datetime_today(self, tz):
673668

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

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

0 commit comments

Comments
 (0)