Skip to content

Commit 458c290

Browse files
anmyachevyhaque1213
authored andcommitted
BUG: pd.to_datetime() throws if caching is on with Null-like arguments (pandas-dev#26078)
1 parent 177e7b9 commit 458c290

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Datetimelike
267267
^^^^^^^^^^^^
268268

269269
- 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`)
270-
-
270+
- Bug in :func:`to_datetime` which would raise ``InvalidIndexError: Reindexing only valid with uniquely valued Index objects`` when called with ``cache=True``, with ``arg`` including at least two different elements from the set {None, numpy.nan, pandas.NaT} (:issue:`22305`)
271271
-
272272
-
273273

pandas/core/tools/datetimes.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ def _maybe_cache(arg, format, cache, convert_listlike):
5252
if cache:
5353
# Perform a quicker unique check
5454
from pandas import Index
55-
if not Index(arg).is_unique:
56-
unique_dates = algorithms.unique(arg)
57-
cache_dates = convert_listlike(unique_dates, True, format)
55+
unique_dates = Index(arg).unique()
56+
if len(unique_dates) < len(arg):
57+
cache_dates = convert_listlike(unique_dates.to_numpy(),
58+
True, format)
5859
cache_array = Series(cache_dates, index=unique_dates)
5960
return cache_array
6061

pandas/tests/indexes/datetimes/test_tools.py

+9
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,15 @@ def test_parsers(self, date_str, expected, cache):
16281628
yearfirst=yearfirst)
16291629
assert result7 == expected
16301630

1631+
@pytest.mark.parametrize('cache', [True, False])
1632+
def test_na_values_with_cache(self, cache, unique_nulls_fixture,
1633+
unique_nulls_fixture2):
1634+
# GH22305
1635+
expected = Index([NaT, NaT], dtype='datetime64[ns]')
1636+
result = to_datetime([unique_nulls_fixture, unique_nulls_fixture2],
1637+
cache=cache)
1638+
tm.assert_index_equal(result, expected)
1639+
16311640
def test_parsers_nat(self):
16321641
# Test that each of several string-accepting methods return pd.NaT
16331642
result1, _, _ = parsing.parse_time_string('NaT')

0 commit comments

Comments
 (0)