Skip to content

Commit ed2dca6

Browse files
author
MarcoGorelli
committed
empty strings -> nat
1 parent 5accaaa commit ed2dca6

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ Datetimelike
776776
- Bug in rendering :class:`DatetimeIndex` and :class:`Series` and :class:`DataFrame` with timezone-aware dtypes with ``dateutil`` or ``zoneinfo`` timezones near daylight-savings transitions (:issue:`49684`)
777777
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing :class:`Timestamp`, ``datetime.datetime``, ``datetime.date``, or ``np.datetime64`` objects when non-ISO8601 ``format`` was passed (:issue:`49298`, :issue:`50036`)
778778
- Bug in :class:`Timestamp` was showing ``UserWarning`` which was not actionable by users (:issue:`50232`)
779+
- Bug in :func:`to_datetime` was raising ``ValueError`` when parsing empty string and non-ISO8601 format was passed. Now, empty strings will be parsed as :class:`NaT`, for compatibility with how is done for ISO8601 formats (:issue:`50251`)
779780
-
780781

781782
Timedelta

pandas/_libs/tslibs/strptime.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def array_strptime(
163163
for i in range(n):
164164
val = values[i]
165165
if isinstance(val, str):
166-
if val in nat_strings:
166+
if len(val) == 0 or val in nat_strings:
167167
iresult[i] = NPY_NAT
168168
continue
169169
elif checknull_with_nat_and_na(val):

pandas/tests/tools/test_to_datetime.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,17 +2033,13 @@ def test_to_datetime_timezone_name(self):
20332033
assert result == expected
20342034

20352035
@td.skip_if_not_us_locale
2036-
def test_to_datetime_with_apply_with_empty_str(self, cache):
2036+
@pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"])
2037+
def test_to_datetime_with_apply_with_empty_str(self, cache, errors):
20372038
# this is only locale tested with US/None locales
2038-
# GH 5195
2039+
# GH 5195, GH50251
20392040
# with a format and coerce a single item to_datetime fails
20402041
td = Series(["May 04", "Jun 02", ""], index=[1, 2, 3])
2041-
msg = r"time data '' does not match format '%b %y' \(match\)"
2042-
with pytest.raises(ValueError, match=msg):
2043-
to_datetime(td, format="%b %y", errors="raise", cache=cache)
2044-
with pytest.raises(ValueError, match=msg):
2045-
td.apply(to_datetime, format="%b %y", errors="raise", cache=cache)
2046-
expected = to_datetime(td, format="%b %y", errors="coerce", cache=cache)
2042+
expected = to_datetime(td, format="%b %y", errors=errors, cache=cache)
20472043

20482044
result = td.apply(
20492045
lambda x: to_datetime(x, format="%b %y", errors="coerce", cache=cache)
@@ -2987,24 +2983,17 @@ def test_na_to_datetime(nulls_fixture, klass):
29872983
assert result[0] is NaT
29882984

29892985

2990-
def test_empty_string_datetime_coerce_format():
2991-
# GH13044
2986+
@pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"])
2987+
def test_empty_string_datetime_coerce_format(errors):
2988+
# GH13044, GH50251
29922989
td = Series(["03/24/2016", "03/25/2016", ""])
29932990
format = "%m/%d/%Y"
29942991

29952992
# coerce empty string to pd.NaT
2996-
result = to_datetime(td, format=format, errors="coerce")
2993+
result = to_datetime(td, format=format, errors=errors)
29972994
expected = Series(["2016-03-24", "2016-03-25", NaT], dtype="datetime64[ns]")
29982995
tm.assert_series_equal(expected, result)
29992996

3000-
# raise an exception in case a format is given
3001-
with pytest.raises(ValueError, match="does not match format"):
3002-
to_datetime(td, format=format, errors="raise")
3003-
3004-
# still raise an exception in case no format is given
3005-
with pytest.raises(ValueError, match="does not match format"):
3006-
to_datetime(td, errors="raise")
3007-
30082997

30092998
def test_empty_string_datetime_coerce__unit():
30102999
# GH13044

0 commit comments

Comments
 (0)