Skip to content

BUG: pd.to_datetime() throws if caching is on with Null-like arguments #26078

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 13 commits into from
Apr 17, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Datetimelike
^^^^^^^^^^^^

- Bug in :func:`to_datetime` which would raise an (incorrect) ``ValueError`` when called with a date far into the future and the ``format`` argument specified instead of raising ``OutOfBoundsDatetime`` (:issue:`23830`)
-
- Bug in :func:`to_datetime` which would raise ``InvalidIndexError`` when called with ``cache=True``, with ``arg`` including several different "not a numbers" (:issue:`22305`)
-
-

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ def _maybe_cache(arg, format, cache, convert_listlike):
from pandas import Series
cache_array = Series()
if cache:
# Perform a quicker unique check
from pandas import Index
if not Index(arg).is_unique:
unique_dates = algorithms.unique(arg)
cache_dates = convert_listlike(unique_dates, True, format)
# GH26078
unique_dates = Index(arg).unique()
if len(unique_dates) < len(arg):
cache_dates = convert_listlike(unique_dates.to_numpy(),
True, format)
cache_array = Series(cache_dates, index=unique_dates)
return cache_array

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,15 @@ def test_parsers(self, date_str, expected, cache):
yearfirst=yearfirst)
assert result7 == expected

@pytest.mark.parametrize('cache', [True, False])
def test_na_values_with_cache(self, cache, unique_nulls_fixture,
unique_nulls_fixture2):
# GH22305
expected = Index([NaT, NaT], dtype='datetime64[ns]')
result = to_datetime([unique_nulls_fixture, unique_nulls_fixture2],
cache=cache)
tm.assert_index_equal(result, expected)

def test_parsers_nat(self):
# Test that each of several string-accepting methods return pd.NaT
result1, _, _ = parsing.parse_time_string('NaT')
Expand Down