Skip to content

Commit ad6283c

Browse files
maheshrbapatuproost
authored andcommitted
Fixes Error thrown when None is passed to pd.to_datetime() with format as %Y%m%d (pandas-dev#30083)
1 parent cad35ba commit ad6283c

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ Datetimelike
693693
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
694694
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
695695
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
696+
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)
696697

697698
Timedelta
698699
^^^^^^^^^

pandas/core/tools/datetimes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -883,21 +883,21 @@ def calc_with_mask(carg, mask):
883883
# try intlike / strings that are ints
884884
try:
885885
return calc(arg.astype(np.int64))
886-
except (ValueError, OverflowError):
886+
except (ValueError, OverflowError, TypeError):
887887
pass
888888

889889
# a float with actual np.nan
890890
try:
891891
carg = arg.astype(np.float64)
892892
return calc_with_mask(carg, notna(carg))
893-
except (ValueError, OverflowError):
893+
except (ValueError, OverflowError, TypeError):
894894
pass
895895

896896
# string with NaN-like
897897
try:
898898
mask = ~algorithms.isin(arg, list(tslib.nat_strings))
899899
return calc_with_mask(arg, mask)
900-
except (ValueError, OverflowError):
900+
except (ValueError, OverflowError, TypeError):
901901
pass
902902

903903
return None

pandas/tests/indexes/datetimes/test_tools.py

+23
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,29 @@ def test_to_datetime_format_YYYYMMDD(self, cache):
101101
expected = Series(["20121231", "20141231", "NaT"], dtype="M8[ns]")
102102
tm.assert_series_equal(result, expected)
103103

104+
@pytest.mark.parametrize(
105+
"input_s",
106+
[
107+
# Null values with Strings
108+
["19801222", "20010112", None],
109+
["19801222", "20010112", np.nan],
110+
["19801222", "20010112", pd.NaT],
111+
["19801222", "20010112", "NaT"],
112+
# Null values with Integers
113+
[19801222, 20010112, None],
114+
[19801222, 20010112, np.nan],
115+
[19801222, 20010112, pd.NaT],
116+
[19801222, 20010112, "NaT"],
117+
],
118+
)
119+
def test_to_datetime_format_YYYYMMDD_with_none(self, input_s):
120+
# GH 30011
121+
# format='%Y%m%d'
122+
# with None
123+
expected = Series([Timestamp("19801222"), Timestamp("20010112"), pd.NaT])
124+
result = Series(pd.to_datetime(input_s, format="%Y%m%d"))
125+
tm.assert_series_equal(result, expected)
126+
104127
@pytest.mark.parametrize(
105128
"input_s, expected",
106129
[

0 commit comments

Comments
 (0)