Skip to content

Fixes Error thrown when None is passed to pd.to_datetime() with format as %Y%m%d #30083

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 16 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
704516f
Closes #30011
maheshreddybapatu Dec 5, 2019
5351805
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 5, 2019
4d6e052
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 5, 2019
362d6e5
Merge branch 'none_date_time' of https://github.com/maheshbapatu/pand…
maheshreddybapatu Dec 7, 2019
de77f02
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
a2faab3
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
4dc5a3a
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
d6297b2
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
7157af5
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
2a5a687
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
6aee7ce
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
5c1d919
This avoids failure to_datetime when value passed is None. This fix i…
maheshreddybapatu Dec 7, 2019
46cd6d0
This avoids failure to_datetime when value passed is None to to_datet…
maheshreddybapatu Dec 8, 2019
c07c1cf
This avoids failure in to_datetime when value passed is None. After c…
maheshreddybapatu Dec 8, 2019
13c5c42
This avoids failure in to_datetime when value passed is None.
maheshreddybapatu Dec 8, 2019
a47da79
This avoids failure in to_datetime when value passed is Null type.
maheshreddybapatu Dec 10, 2019
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/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ Datetimelike
- Bug in :meth:`Series.var` failing to raise ``TypeError`` when called with ``timedelta64[ns]`` dtype (:issue:`28289`)
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)

Timedelta
^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,21 +940,21 @@ def calc_with_mask(carg, mask):
# try intlike / strings that are ints
try:
return calc(arg.astype(np.int64))
except (ValueError, OverflowError):
except (ValueError, OverflowError, TypeError):
pass

# a float with actual np.nan
try:
carg = arg.astype(np.float64)
return calc_with_mask(carg, notna(carg))
except (ValueError, OverflowError):
except (ValueError, OverflowError, TypeError):
pass

# string with NaN-like
try:
mask = ~algorithms.isin(arg, list(tslib.nat_strings))
return calc_with_mask(arg, mask)
except (ValueError, OverflowError):
except (ValueError, OverflowError, TypeError):
pass

return None
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ def test_to_datetime_format_YYYYMMDD(self, cache):
expected = Series(["20121231", "20141231", "NaT"], dtype="M8[ns]")
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"input_s",
[
# None with Strings
Series(["19801222", None, "20010112", np.nan, "NaT", pd.NaT]),
# None with Integers
Series([19801222, None, 20010112, np.nan, "NaT", pd.NaT]),
],
)
def test_to_datetime_format_YYYYMMDD_with_none(self, input_s):
# GH 30011
# format='%Y%m%d'
# with None
expected = Series(
[
Timestamp("19801222"),
Timestamp(None),
Timestamp("20010112"),
Timestamp(np.nan),
Timestamp("NaT"),
Timestamp(pd.NaT),
]
)
result = pd.to_datetime(input_s, format="%Y%m%d")
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"input_s, expected",
[
Expand Down