Skip to content

COMPAT: should an empty string match a format (or just be NaT) #37835

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 5 commits into from
Nov 19, 2020
Merged
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2443,3 +2443,34 @@ def test_na_to_datetime(nulls_fixture, klass):
result = pd.to_datetime(klass([nulls_fixture]))

assert result[0] is pd.NaT


def test_empty_string_datetime_coerce__format():
# GH13044
td = Series(["03/24/2016", "03/25/2016", ""])
format = "%m/%d/%Y"

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

# raise an exception in case a format is given
with pytest.raises(ValueError, match="does not match format"):
result = pd.to_datetime(td, format=format, errors="raise")

# don't raise an expection in case no format is given
result = pd.to_datetime(td, errors="raise")
tm.assert_series_equal(result, expected)


def test_empty_string_datetime_coerce__unit():
# GH13044
# coerce empty string to pd.NaT
result = pd.to_datetime([1, ""], unit="s", errors="coerce")
expected = DatetimeIndex(["1970-01-01 00:00:01", "NaT"], dtype="datetime64[ns]")
tm.assert_index_equal(expected, result)

# verify that no exception is raised even when errors='raise' is set
result = pd.to_datetime([1, ""], unit="s", errors="raise")
tm.assert_index_equal(expected, result)