Skip to content

Commit ae22dce

Browse files
committed
BUG: String indexing against object dtype may raise AttributeError
1 parent 7d40f18 commit ae22dce

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/source/whatsnew/v0.19.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bug Fixes
3737
- Bug in localizing an ambiguous timezone when a boolean is passed (:issue:`14402`)
3838

3939

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

4142

4243

pandas/indexes/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,11 @@ def _wrap_joined_index(self, joined, other):
29362936
name = self.name if self.name == other.name else None
29372937
return Index(joined, name=name)
29382938

2939+
def _get_string_slice(self, key, use_lhs=True, use_rhs=True):
2940+
# this is for partial string indexing,
2941+
# overridden in DatetimeIndex, TimedeltaIndex and PeriodIndex
2942+
raise NotImplementedError
2943+
29392944
def slice_indexer(self, start=None, end=None, step=None, kind=None):
29402945
"""
29412946
For an ordered Index, compute the slice indexer for input labels and

pandas/tests/indexing/test_indexing.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,27 @@ def test_iloc_non_unique_indexing(self):
36133613
result = df2.loc[idx]
36143614
tm.assert_frame_equal(result, expected, check_index_type=False)
36153615

3616+
def test_string_slice(self):
3617+
# GH 14424
3618+
# string indexing against datetimelike with object
3619+
# dtype should properly raises KeyError
3620+
df = pd.DataFrame([1], pd.Index([pd.Timestamp('2011-01-01')],
3621+
dtype=object))
3622+
self.assertTrue(df.index.is_all_dates)
3623+
with tm.assertRaises(KeyError):
3624+
df['2011']
3625+
3626+
with tm.assertRaises(KeyError):
3627+
df.loc['2011', 0]
3628+
3629+
df = pd.DataFrame()
3630+
self.assertFalse(df.index.is_all_dates)
3631+
with tm.assertRaises(KeyError):
3632+
df['2011']
3633+
3634+
with tm.assertRaises(KeyError):
3635+
df.loc['2011', 0]
3636+
36163637
def test_mi_access(self):
36173638

36183639
# GH 4145

0 commit comments

Comments
 (0)