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
12 changes: 11 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
unique='IndexOpsMixin', duplicated='IndexOpsMixin')


class InvalidIndexError(Exception):
pass


class StringMixin(object):
"""implements string methods so long as object defines a `__unicode__`
method.
Expand Down Expand Up @@ -1201,7 +1205,13 @@ def _map_values(self, mapper, na_action=None):
else:
values = self.values

indexer = mapper.index.get_indexer(values)
try:
indexer = mapper.index.get_indexer(values)
except InvalidIndexError:
from pandas import Series
mapper = Series(algorithms.unique(mapper))
indexer = mapper.index.get_indexer(values)

new_values = algorithms.take_1d(mapper._values, indexer)

return new_values
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from pandas.core.accessor import CachedAccessor, DirNamesMixin
import pandas.core.algorithms as algos
from pandas.core.arrays import ExtensionArray
from pandas.core.base import IndexOpsMixin, PandasObject
from pandas.core.base import IndexOpsMixin, InvalidIndexError, PandasObject
import pandas.core.common as com
from pandas.core.indexes.frozen import FrozenList
import pandas.core.missing as missing
Expand Down Expand Up @@ -143,10 +143,6 @@ def index_arithmetic_method(self, other):
return set_function_name(index_arithmetic_method, name, cls)


class InvalidIndexError(Exception):
pass


_o_dtype = np.dtype(object)
_Identity = object

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def _simple_ts(start, end, freq='D'):
return Series(np.random.randn(len(rng)), index=rng)


def test_NA_values_with_cache():
# GH 22305
na_values = [None, np.nan, pd.NaT]
# check pairwise, that no pair of na values
# is mangled
for f in na_values:
for s in na_values:
if f is not s: # otherwise not unique
expected = Index([NaT, NaT], dtype='datetime64[ns]')
result = to_datetime([f, s], cache=True)
tm.assert_index_equal(result, expected)


def assert_range_equal(left, right):
assert (left.equals(right))
assert (left.freq == right.freq)
Expand Down