Skip to content

Commit 437aa8b

Browse files
authored
COMPAT: should an empty string match a format (or just be NaT) (pandas-dev#37835)
1 parent 6ab69e0 commit 437aa8b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pandas/tests/tools/test_to_datetime.py

+31
Original file line numberDiff line numberDiff line change
@@ -2443,3 +2443,34 @@ def test_na_to_datetime(nulls_fixture, klass):
24432443
result = pd.to_datetime(klass([nulls_fixture]))
24442444

24452445
assert result[0] is pd.NaT
2446+
2447+
2448+
def test_empty_string_datetime_coerce__format():
2449+
# GH13044
2450+
td = Series(["03/24/2016", "03/25/2016", ""])
2451+
format = "%m/%d/%Y"
2452+
2453+
# coerce empty string to pd.NaT
2454+
result = pd.to_datetime(td, format=format, errors="coerce")
2455+
expected = Series(["2016-03-24", "2016-03-25", pd.NaT], dtype="datetime64[ns]")
2456+
tm.assert_series_equal(expected, result)
2457+
2458+
# raise an exception in case a format is given
2459+
with pytest.raises(ValueError, match="does not match format"):
2460+
result = pd.to_datetime(td, format=format, errors="raise")
2461+
2462+
# don't raise an expection in case no format is given
2463+
result = pd.to_datetime(td, errors="raise")
2464+
tm.assert_series_equal(result, expected)
2465+
2466+
2467+
def test_empty_string_datetime_coerce__unit():
2468+
# GH13044
2469+
# coerce empty string to pd.NaT
2470+
result = pd.to_datetime([1, ""], unit="s", errors="coerce")
2471+
expected = DatetimeIndex(["1970-01-01 00:00:01", "NaT"], dtype="datetime64[ns]")
2472+
tm.assert_index_equal(expected, result)
2473+
2474+
# verify that no exception is raised even when errors='raise' is set
2475+
result = pd.to_datetime([1, ""], unit="s", errors="raise")
2476+
tm.assert_index_equal(expected, result)

0 commit comments

Comments
 (0)