diff --git a/doc/source/whatsnew/v0.19.1.txt b/doc/source/whatsnew/v0.19.1.txt index 2a421e5d2eacd..6dada92099a7f 100644 --- a/doc/source/whatsnew/v0.19.1.txt +++ b/doc/source/whatsnew/v0.19.1.txt @@ -37,6 +37,7 @@ Bug Fixes - Bug in localizing an ambiguous timezone when a boolean is passed (:issue:`14402`) +- Bug in string indexing against data with ``object`` ``Index`` may raise ``AttributeError`` (:issue:`14424`) diff --git a/pandas/indexes/base.py b/pandas/indexes/base.py index 5082fc84982c6..8b3ec86a44f11 100644 --- a/pandas/indexes/base.py +++ b/pandas/indexes/base.py @@ -2936,6 +2936,11 @@ def _wrap_joined_index(self, joined, other): name = self.name if self.name == other.name else None return Index(joined, name=name) + def _get_string_slice(self, key, use_lhs=True, use_rhs=True): + # this is for partial string indexing, + # overridden in DatetimeIndex, TimedeltaIndex and PeriodIndex + raise NotImplementedError + def slice_indexer(self, start=None, end=None, step=None, kind=None): """ For an ordered Index, compute the slice indexer for input labels and diff --git a/pandas/tests/indexing/test_indexing.py b/pandas/tests/indexing/test_indexing.py index fa406a27bef69..a50d3d28e5a11 100644 --- a/pandas/tests/indexing/test_indexing.py +++ b/pandas/tests/indexing/test_indexing.py @@ -3613,6 +3613,27 @@ def test_iloc_non_unique_indexing(self): result = df2.loc[idx] tm.assert_frame_equal(result, expected, check_index_type=False) + def test_string_slice(self): + # GH 14424 + # string indexing against datetimelike with object + # dtype should properly raises KeyError + df = pd.DataFrame([1], pd.Index([pd.Timestamp('2011-01-01')], + dtype=object)) + self.assertTrue(df.index.is_all_dates) + with tm.assertRaises(KeyError): + df['2011'] + + with tm.assertRaises(KeyError): + df.loc['2011', 0] + + df = pd.DataFrame() + self.assertFalse(df.index.is_all_dates) + with tm.assertRaises(KeyError): + df['2011'] + + with tm.assertRaises(KeyError): + df.loc['2011', 0] + def test_mi_access(self): # GH 4145