Skip to content

Commit 1feacde

Browse files
authored
BUG: to_datetime(None) inconsistent with other conversions (#62249)
1 parent ade778e commit 1feacde

File tree

6 files changed

+19
-5
lines changed

6 files changed

+19
-5
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ Conversion
921921
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
922922
- Bug in :meth:`Series.convert_dtypes` and :meth:`DataFrame.convert_dtypes` removing timezone information for objects with :class:`ArrowDtype` (:issue:`60237`)
923923
- Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`)
924+
- Bug in :meth:`to_datetime` and :meth:`to_timedelta` with input ``None`` returning ``None`` instead of ``NaT``, inconsistent with other conversion methods (:issue:`23055`)
924925

925926
Strings
926927
^^^^^^^

pandas/core/generic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10519,8 +10519,12 @@ def truncate(
1051910519
if ax._is_all_dates:
1052010520
from pandas.core.tools.datetimes import to_datetime
1052110521

10522-
before = to_datetime(before)
10523-
after = to_datetime(after)
10522+
if before is not None:
10523+
# Avoid converting to NaT
10524+
before = to_datetime(before)
10525+
if after is not None:
10526+
# Avoid converting to NaT
10527+
after = to_datetime(after)
1052410528

1052510529
if before is not None and after is not None and before > after:
1052610530
raise ValueError(f"Truncate: {after} must be after {before}")

pandas/core/tools/datetimes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
tslib,
2222
)
2323
from pandas._libs.tslibs import (
24+
NaT,
2425
OutOfBoundsDatetime,
2526
Timedelta,
2627
Timestamp,
@@ -676,7 +677,7 @@ def to_datetime(
676677
unit: str | None = None,
677678
origin: str = "unix",
678679
cache: bool = True,
679-
) -> DatetimeIndex | Series | DatetimeScalar | NaTType | None:
680+
) -> DatetimeIndex | Series | DatetimeScalar | NaTType:
680681
"""
681682
Convert argument to datetime.
682683
@@ -989,7 +990,7 @@ def to_datetime(
989990
if exact is not lib.no_default and format in {"mixed", "ISO8601"}:
990991
raise ValueError("Cannot use 'exact' when 'format' is 'mixed' or 'ISO8601'")
991992
if arg is None:
992-
return None
993+
return NaT
993994

994995
if origin != "unix":
995996
arg = _adjust_to_origin(arg, origin, unit)

pandas/core/tools/timedeltas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def to_timedelta(
182182
raise ValueError("errors must be one of 'raise', or 'coerce'.")
183183

184184
if arg is None:
185-
return arg
185+
return NaT
186186
elif isinstance(arg, ABCSeries):
187187
values = _convert_listlike(arg._values, unit=unit, errors=errors)
188188
return arg._constructor(values, index=arg.index, name=arg.name)

pandas/tests/tools/test_to_datetime.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ def test_to_datetime_parse_timezone_keeps_name(self):
531531

532532

533533
class TestToDatetime:
534+
def test_to_datetime_none(self):
535+
# GH#23055
536+
assert to_datetime(None) is NaT
537+
534538
@pytest.mark.filterwarnings("ignore:Could not infer format")
535539
def test_to_datetime_overflow(self):
536540
# we should get an OutOfBoundsDatetime, NOT OverflowError

pandas/tests/tools/test_to_timedelta.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828

2929
class TestTimedeltas:
30+
def test_to_timedelta_none(self):
31+
# GH#23055
32+
assert to_timedelta(None) is pd.NaT
33+
3034
def test_to_timedelta_dt64_raises(self):
3135
# Passing datetime64-dtype data to TimedeltaIndex is no longer
3236
# supported GH#29794

0 commit comments

Comments
 (0)