Skip to content

Commit 3574a4a

Browse files
committed
BUG: Bug in multi-index selection in PY3 when using certain keys (GH5725)
1 parent 1a68b73 commit 3574a4a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ Bug Fixes
826826
- Bug in repeated indexing of object with resultant non-unique index (:issue:`5678`)
827827
- Bug in fillna with Series and a passed series/dict (:issue:`5703`)
828828
- Bug in groupby transform with a datetime-like grouper (:issue:`5712`)
829+
- Bug in multi-index selection in PY3 when using certain keys (:issue:`5725`)
829830

830831
pandas 0.12.0
831832
-------------

pandas/core/index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,8 +2300,14 @@ def _try_mi(k):
23002300

23012301
# a Timestamp will raise a TypeError in a multi-index
23022302
# rather than a KeyError, try it here
2303+
# note that a string that 'looks' like a Timestamp will raise
2304+
# a KeyError! (GH5725)
23032305
if isinstance(key, (datetime.datetime, np.datetime64)) or (
23042306
compat.PY3 and isinstance(key, compat.string_types)):
2307+
try:
2308+
return _try_mi(key)
2309+
except:
2310+
pass
23052311
try:
23062312
return _try_mi(Timestamp(key))
23072313
except:

pandas/tests/test_indexing.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def test_indexer_caching(self):
253253
# GH5727
254254
# make sure that indexers are in the _internal_names_set
255255
n = 1000001
256-
arrays = [lrange(n), np.empty(n)]
256+
arrays = [lrange(n), lrange(n)]
257257
index = MultiIndex.from_tuples(lzip(*arrays))
258258
s = Series(np.zeros(n), index=index)
259259
str(s)
@@ -839,6 +839,20 @@ def test_xs_multiindex(self):
839839
expected.columns = expected.columns.droplevel('lvl1')
840840
assert_frame_equal(result, expected)
841841

842+
def test_getitem_multiindex(self):
843+
844+
# GH 5725
845+
# the 'A' happens to be a valid Timestamp so the doesn't raise the appropriate
846+
# error, only in PY3 of course!
847+
index = MultiIndex(levels=[['A', 'B', 'C'], [0, 26, 27, 37, 57, 67, 75, 82]],
848+
labels=[[0, 0, 0, 1, 2, 2, 2, 2, 2, 2], [1, 3, 4, 6, 0, 2, 2, 3, 5, 7]],
849+
names=['tag', 'day'])
850+
arr = np.random.randn(len(index),1)
851+
df = DataFrame(arr,index=index,columns=['val'])
852+
result = df.val['A']
853+
expected = Series(arr.ravel()[0:3],name='val',index=Index([26,37,57],name='day'))
854+
assert_series_equal(result,expected)
855+
842856
def test_setitem_dtype_upcast(self):
843857

844858
# GH3216

0 commit comments

Comments
 (0)