Skip to content

Commit 1d74e1e

Browse files
committed
Check to_datetime("now") and to_datetime("today")
According to pandas-devGH-18705 to_datetime("now") and to_datetime("today") must match semantics of Timestamp.now() and Timestamp.today().
1 parent 691e2a9 commit 1d74e1e

File tree

1 file changed

+11
-38
lines changed

1 file changed

+11
-38
lines changed

pandas/tests/tools/test_to_datetime.py

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -544,63 +544,36 @@ def test_to_datetime_unparseable_ignore(self):
544544

545545
@td.skip_if_windows # `tm.set_timezone` does not work in windows
546546
def test_to_datetime_now(self):
547-
# See GH#18666
547+
# See GH#18705, to_datetime("now") matches Timestamp.now()
548548
with tm.set_timezone("US/Eastern"):
549-
npnow = np.datetime64("now").astype("datetime64[ns]")
549+
tsnow = Timestamp.now()
550550
pdnow = to_datetime("now")
551551
pdnow2 = to_datetime(["now"])[0]
552552

553553
# These should all be equal with infinite perf; this gives
554-
# a generous margin of 10 seconds
555-
assert abs(pdnow.value - npnow.astype(np.int64)) < 1e10
556-
assert abs(pdnow2.value - npnow.astype(np.int64)) < 1e10
554+
# a generous margin of 1 seconds
555+
assert abs(pdnow.value - tsnow.value) < 1e9
556+
assert abs(pdnow2.value - tsnow.value) < 1e9
557557

558558
assert pdnow.tzinfo is None
559559
assert pdnow2.tzinfo is None
560560

561561
@td.skip_if_windows # `tm.set_timezone` does not work in windows
562562
def test_to_datetime_today(self):
563-
# See GH#18666
564-
# Test with one timezone far ahead of UTC and another far behind, so
565-
# one of these will _almost_ always be in a different day from UTC.
566-
# Unfortunately this test between 12 and 1 AM Samoa time
567-
# this both of these timezones _and_ UTC will all be in the same day,
568-
# so this test will not detect the regression introduced in #18666.
569-
with tm.set_timezone("Pacific/Auckland"): # 12-13 hours ahead of UTC
570-
nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64)
571-
pdtoday = to_datetime("today")
572-
pdtoday2 = to_datetime(["today"])[0]
573-
574-
tstoday = Timestamp("today")
575-
tstoday2 = Timestamp.today()
576-
577-
# These should all be equal with infinite perf; this gives
578-
# a generous margin of 10 seconds
579-
assert abs(pdtoday.normalize().value - nptoday) < 1e10
580-
assert abs(pdtoday2.normalize().value - nptoday) < 1e10
581-
assert abs(pdtoday.value - tstoday.value) < 1e10
582-
assert abs(pdtoday.value - tstoday2.value) < 1e10
583-
584-
assert pdtoday.tzinfo is None
585-
assert pdtoday2.tzinfo is None
586-
587-
with tm.set_timezone("US/Samoa"): # 11 hours behind UTC
588-
nptoday = np.datetime64("today").astype("datetime64[ns]").astype(np.int64)
563+
# See GH##18705, to_datetime("today") matches Timestamp.today()
564+
with tm.set_timezone("Pacific/Auckland"):
565+
tstoday = Timestamp.today()
589566
pdtoday = to_datetime("today")
590567
pdtoday2 = to_datetime(["today"])[0]
591568

592569
# These should all be equal with infinite perf; this gives
593-
# a generous margin of 10 seconds
594-
assert abs(pdtoday.normalize().value - nptoday) < 1e10
595-
assert abs(pdtoday2.normalize().value - nptoday) < 1e10
570+
# a generous margin of 1 seconds
571+
assert abs(pdtoday.value - tstoday.value) < 1e9
572+
assert abs(pdtoday2.value - tstoday.value) < 1e9
596573

597574
assert pdtoday.tzinfo is None
598575
assert pdtoday2.tzinfo is None
599576

600-
def test_to_datetime_today_now_unicode_bytes(self):
601-
to_datetime(["now"])
602-
to_datetime(["today"])
603-
604577
@pytest.mark.parametrize("cache", [True, False])
605578
def test_to_datetime_dt64s(self, cache):
606579
in_bound_dts = [np.datetime64("2000-01-01"), np.datetime64("2000-01-02")]

0 commit comments

Comments
 (0)