Skip to content

Commit 479741e

Browse files
author
MarcoGorelli
committed
empty strings -> nat
1 parent 113bdb3 commit 479741e

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
@@ -153,7 +153,7 @@ def array_strptime(
153153
for i in range(n):
154154
val = values[i]
155155
if isinstance(val, str):
156-
if val in nat_strings:
156+
if len(val) == 0 or val in nat_strings:
157157
iresult[i] = NPY_NAT
158158
continue
159159
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
@@ -2976,24 +2976,17 @@ def test_na_to_datetime(nulls_fixture, klass):
29762976
assert result[0] is NaT
29772977

29782978

2979-
def test_empty_string_datetime_coerce_format():
2980-
# GH13044
2979+
@pytest.mark.parametrize("errors", ["raise", "coerce", "ignore"])
2980+
def test_empty_string_datetime_coerce_format(errors):
2981+
# GH13044, GH50251
29812982
td = Series(["03/24/2016", "03/25/2016", ""])
29822983
format = "%m/%d/%Y"
29832984

29842985
# coerce empty string to pd.NaT
2985-
result = to_datetime(td, format=format, errors="coerce")
2986+
result = to_datetime(td, format=format, errors=errors)
29862987
expected = Series(["2016-03-24", "2016-03-25", NaT], dtype="datetime64[ns]")
29872988
tm.assert_series_equal(expected, result)
29882989

2989-
# raise an exception in case a format is given
2990-
with pytest.raises(ValueError, match="does not match format"):
2991-
to_datetime(td, format=format, errors="raise")
2992-
2993-
# still raise an exception in case no format is given
2994-
with pytest.raises(ValueError, match="does not match format"):
2995-
to_datetime(td, errors="raise")
2996-
29972990

29982991
def test_empty_string_datetime_coerce__unit():
29992992
# GH13044

0 commit comments

Comments
 (0)