Skip to content

BUG: Regression in chained getitem indexing with embedded list-like from 0.12 (GH6394) #6396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Bug Fixes
- Bug in Series.get, was using a buggy access method (:issue:`6383`)
- Bug in hdfstore queries of the form ``where=[('date', '>=', datetime(2013,1,1)), ('date', '<=', datetime(2014,1,1))]`` (:issue:`6313`)
- Bug in DataFrame.dropna with duplicate indices (:issue:`6355`)
- Regression in chained getitem indexing with embedded list-like from 0.12 (:issue:`6394`)

pandas 0.13.1
-------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,7 +1312,11 @@ def xs(self, key, axis=0, level=None, copy=True, drop_level=True):
new_values, copy = self._data.fast_xs(loc, copy=copy)

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

result = Series(new_values, index=self.columns,
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,15 @@ def _slice(self, slobj, axis=0, raise_on_error=False, typ=None):
def __getitem__(self, key):
try:
result = self.index.get_value(self, key)
if isinstance(result, np.ndarray):
return self._constructor(result,index=[key]*len(result)).__finalize__(self)

if not np.isscalar(result):
if is_list_like(result) and not isinstance(result, Series):

# we need to box if we have a non-unique index here
# otherwise have inline ndarray/lists
if not self.index.is_unique:
result = self._constructor(result,index=[key]*len(result)).__finalize__(self)

return result
except InvalidIndexError:
pass
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,26 @@ def test_loc_setitem(self):
expected = DataFrame({'a' : [0.5,-0.5,-1.5], 'b' : [0,1,2] })
assert_frame_equal(df,expected)

def test_chained_getitem_with_lists(self):

# GH6394
# Regression in chained getitem indexing with embedded list-like from 0.12
def check(result, expected):
self.assert_numpy_array_equal(result,expected)
tm.assert_isinstance(result, np.ndarray)


df = DataFrame({'A': 5*[np.zeros(3)], 'B':5*[np.ones(3)]})
expected = df['A'].iloc[2]
result = df.loc[2,'A']
check(result, expected)
result2 = df.iloc[2]['A']
check(result2, expected)
result3 = df['A'].loc[2]
check(result3, expected)
result4 = df['A'].iloc[2]
check(result4, expected)

def test_loc_getitem_int(self):

# int label
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,12 @@ def test_getitem_dups_with_missing(self):
result = s[['foo', 'bar', 'bah', 'bam']]
assert_series_equal(result, expected)

def test_getitem_dups(self):
s = Series(range(5),index=['A','A','B','C','C'])
expected = Series([3,4],index=['C','C'])
result = s['C']
assert_series_equal(result, expected)

def test_setitem_ambiguous_keyerror(self):
s = Series(lrange(10), index=lrange(0, 20, 2))

Expand Down Expand Up @@ -4813,6 +4819,7 @@ def test_apply_args(self):

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

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