Skip to content

BUG: fix fancy indexing with empty list #6551

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
Mar 6, 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 @@ -207,6 +207,7 @@ Bug Fixes
- Bug in ``pd.read_stata`` which would use the wrong data types and missing values (:issue:`6327`)
- Bug in ``DataFrame.to_stata`` that lead to data loss in certain cases, and could exported using the
wrong data types and missing values (:issue:`6335`)
- Bug in indexing: empty list lookup caused ``IndexError`` exceptions (:issue:`6536`, :issue:`6551`)


pandas 0.13.1
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,10 @@ def _maybe_convert_indices(indices, n):
"""
if isinstance(indices, list):
indices = np.array(indices)
if len(indices) == 0:
# If list is empty, np.array will return float and cause indexing
# errors.
return np.empty(0, dtype=np.int_)

mask = indices < 0
if mask.any():
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 @@ -3175,6 +3175,26 @@ def test_set_ix_out_of_bounds_axis_1(self):
df = pd.DataFrame(randn(5, 2), index=["row%s" % i for i in range(5)], columns=["col%s" % i for i in range(2)])
self.assertRaises(ValueError, df.ix.__setitem__, (0 , 2), 100)

def test_iloc_empty_list_indexer_is_ok(self):
from pandas.util.testing import makeCustomDataframe as mkdf
df = mkdf(5, 2)
assert_frame_equal(df.iloc[:,[]], df.iloc[:, :0]) # vertical empty
assert_frame_equal(df.iloc[[],:], df.iloc[:0, :]) # horizontal empty

# FIXME: fix loc & xs
def test_loc_empty_list_indexer_is_ok(self):
raise nose.SkipTest('loc discards columns names')
from pandas.util.testing import makeCustomDataframe as mkdf
df = mkdf(5, 2)
assert_frame_equal(df.loc[:,[]], df.iloc[:, :0]) # vertical empty
assert_frame_equal(df.loc[[],:], df.iloc[:0, :]) # horizontal empty

def test_ix_empty_list_indexer_is_ok(self):
raise nose.SkipTest('ix discards columns names')
from pandas.util.testing import makeCustomDataframe as mkdf
df = mkdf(5, 2)
assert_frame_equal(df.ix[:,[]], df.iloc[:, :0]) # vertical empty
assert_frame_equal(df.ix[[],:], df.iloc[:0, :]) # horizontal empty

if __name__ == '__main__':
import nose
Expand Down