Skip to content

Commit 0c0c647

Browse files
committed
BUG: Regression in chained getitem indexing with embedded list-like from 0.12 (6394)
1 parent 7cd9496 commit 0c0c647

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ Bug Fixes
135135
- Bug in Series.get, was using a buggy access method (:issue:`6383`)
136136
- Bug in hdfstore queries of the form ``where=[('date', '>=', datetime(2013,1,1)), ('date', '<=', datetime(2014,1,1))]`` (:issue:`6313`)
137137
- Bug in DataFrame.dropna with duplicate indices (:issue:`6355`)
138+
- Regression in chained getitem indexing with embedded list-like from 0.12 (:issue:`6394`)
138139

139140
pandas 0.13.1
140141
-------------

pandas/core/generic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,11 @@ def xs(self, key, axis=0, level=None, copy=True, drop_level=True):
13121312
new_values, copy = self._data.fast_xs(loc, copy=copy)
13131313

13141314
# may need to box a datelike-scalar
1315-
if not is_list_like(new_values):
1315+
#
1316+
# if we encounter an array-like and we only have 1 dim
1317+
# that means that their are list/ndarrays inside the Series!
1318+
# so just return them (GH 6394)
1319+
if not is_list_like(new_values) or self.ndim == 1:
13161320
return _maybe_box_datetimelike(new_values)
13171321

13181322
result = Series(new_values, index=self.columns,

pandas/core/series.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,15 @@ def _slice(self, slobj, axis=0, raise_on_error=False, typ=None):
477477
def __getitem__(self, key):
478478
try:
479479
result = self.index.get_value(self, key)
480-
if isinstance(result, np.ndarray):
481-
return self._constructor(result,index=[key]*len(result)).__finalize__(self)
480+
481+
if not np.isscalar(result):
482+
if is_list_like(result) and not isinstance(result, Series):
483+
484+
# we need to box if we have a non-unique index here
485+
# otherwise have inline ndarray/lists
486+
if not self.index.is_unique:
487+
result = self._constructor(result,index=[key]*len(result)).__finalize__(self)
488+
482489
return result
483490
except InvalidIndexError:
484491
pass

pandas/tests/test_indexing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,26 @@ def test_loc_setitem(self):
551551
expected = DataFrame({'a' : [0.5,-0.5,-1.5], 'b' : [0,1,2] })
552552
assert_frame_equal(df,expected)
553553

554+
def test_chained_getitem_with_lists(self):
555+
556+
# GH6394
557+
# Regression in chained getitem indexing with embedded list-like from 0.12
558+
def check(result, expected):
559+
self.assert_numpy_array_equal(result,expected)
560+
tm.assert_isinstance(result, np.ndarray)
561+
562+
563+
df = DataFrame({'A': 5*[np.zeros(3)], 'B':5*[np.ones(3)]})
564+
expected = df['A'].iloc[2]
565+
result = df.loc[2,'A']
566+
check(result, expected)
567+
result2 = df.iloc[2]['A']
568+
check(result2, expected)
569+
result3 = df['A'].loc[2]
570+
check(result3, expected)
571+
result4 = df['A'].iloc[2]
572+
check(result4, expected)
573+
554574
def test_loc_getitem_int(self):
555575

556576
# int label

pandas/tests/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ def test_getitem_dups_with_missing(self):
949949
result = s[['foo', 'bar', 'bah', 'bam']]
950950
assert_series_equal(result, expected)
951951

952+
def test_getitem_dups(self):
953+
s = Series(range(5),index=['A','A','B','C','C'])
954+
expected = Series([3,4],index=['C','C'])
955+
result = s['C']
956+
assert_series_equal(result, expected)
957+
952958
def test_setitem_ambiguous_keyerror(self):
953959
s = Series(lrange(10), index=lrange(0, 20, 2))
954960

@@ -4813,6 +4819,7 @@ def test_apply_args(self):
48134819

48144820
result = s.apply(str.split, args=(',',))
48154821
self.assertEqual(result[0], ['foo', 'bar'])
4822+
tm.assert_isinstance(result[0], list)
48164823

48174824
def test_align(self):
48184825
def _check_align(a, b, how='left', fill=None):

0 commit comments

Comments
 (0)