Skip to content

Commit 5f38e28

Browse files
committed
BUG: pd.to_datetime() throws if caching is on with Null-like arguments (pandas-dev#26078)
1 parent f41dd55 commit 5f38e28

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pandas/core/tools/datetimes.py

Lines changed: 4 additions & 3 deletions
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,15 @@ def test_parsers(self, date_str, expected, cache):
15191519
yearfirst=yearfirst)
15201520
assert result7 == expected
15211521

1522+
@pytest.mark.parametrize('cache', [True, False])
1523+
def test_na_values_with_cache(self, cache, unique_nulls_fixture,
1524+
unique_nulls_fixture2):
1525+
# GH22305
1526+
expected = Index([NaT, NaT], dtype='datetime64[ns]')
1527+
result = to_datetime([unique_nulls_fixture, unique_nulls_fixture2],
1528+
cache=cache)
1529+
tm.assert_index_equal(result, expected)
1530+
15221531
def test_parsers_nat(self):
15231532
# Test that each of several string-accepting methods return pd.NaT
15241533
result1, _, _ = parsing.parse_time_string('NaT')

0 commit comments

Comments
 (0)