Skip to content

Commit

Permalink
BUG: String indexing against object dtype may raise AttributeError (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhrks authored and jorisvandenbossche committed Oct 22, 2016
1 parent aff20eb commit 233d51d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bug Fixes



- Bug in string indexing against data with ``object`` ``Index`` may raise ``AttributeError`` (:issue:`14424`)



Expand Down
5 changes: 5 additions & 0 deletions pandas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,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
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 233d51d

Please sign in to comment.