Skip to content

Commit 2b6a4cd

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

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
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: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,24 +2987,17 @@ def test_na_to_datetime(nulls_fixture, klass):
29872987
assert result[0] is NaT
29882988

29892989

2990-
def test_empty_string_datetime_coerce_format():
2991-
# GH13044
2990+
@pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"])
2991+
def test_empty_string_datetime_coerce_format(errors):
2992+
# GH13044, GH50251
29922993
td = Series(["03/24/2016", "03/25/2016", ""])
29932994
format = "%m/%d/%Y"
29942995

29952996
# coerce empty string to pd.NaT
2996-
result = to_datetime(td, format=format, errors="coerce")
2997+
result = to_datetime(td, format=format, errors=errors)
29972998
expected = Series(["2016-03-24", "2016-03-25", NaT], dtype="datetime64[ns]")
29982999
tm.assert_series_equal(expected, result)
29993000

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-
30083001

30093002
def test_empty_string_datetime_coerce__unit():
30103003
# GH13044

0 commit comments

Comments
 (0)