Skip to content

Commit 170377d

Browse files
committed
Merge branch 'fix-fancy-indexing-empty-list' of https://github.com/immerrr/pandas into immerrr-fix-fancy-indexing-empty-list
Conflicts: doc/source/release.rst
2 parents 20bc96d + cbd3d5f commit 170377d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/release.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ API Changes
115115
- Better propagation/preservation of Series names when performing groupby
116116
operations:
117117
- ``SeriesGroupBy.agg`` will ensure that the name attribute of the original
118-
series is propagated to the result (:issue:`6265`).
118+
series is propagated to the result (:issue:`6265`).
119119
- If the function provided to ``GroupBy.apply`` returns a named series, the
120120
name of the series will be kept as the name of the column index of the
121121
DataFrame returned by ``GroupBy.apply`` (:issue:`6124`). This facilitates
@@ -218,6 +218,7 @@ Bug Fixes
218218
- Bug in ``DataFrame.to_stata`` that lead to data loss in certain cases, and could exported using the
219219
wrong data types and missing values (:issue:`6335`)
220220
- Inconsistent types in Timestamp addition/subtraction (:issue:`6543`)
221+
- Bug in indexing: empty list lookup caused ``IndexError`` exceptions (:issue:`6536`, :issue:`6551`)
221222

222223

223224
pandas 0.13.1

pandas/core/indexing.py

+4
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,10 @@ def _maybe_convert_indices(indices, n):
15551555
"""
15561556
if isinstance(indices, list):
15571557
indices = np.array(indices)
1558+
if len(indices) == 0:
1559+
# If list is empty, np.array will return float and cause indexing
1560+
# errors.
1561+
return np.empty(0, dtype=np.int_)
15581562

15591563
mask = indices < 0
15601564
if mask.any():

pandas/tests/test_indexing.py

+20
Original file line numberDiff line numberDiff line change
@@ -3175,6 +3175,26 @@ def test_set_ix_out_of_bounds_axis_1(self):
31753175
df = pd.DataFrame(randn(5, 2), index=["row%s" % i for i in range(5)], columns=["col%s" % i for i in range(2)])
31763176
self.assertRaises(ValueError, df.ix.__setitem__, (0 , 2), 100)
31773177

3178+
def test_iloc_empty_list_indexer_is_ok(self):
3179+
from pandas.util.testing import makeCustomDataframe as mkdf
3180+
df = mkdf(5, 2)
3181+
assert_frame_equal(df.iloc[:,[]], df.iloc[:, :0]) # vertical empty
3182+
assert_frame_equal(df.iloc[[],:], df.iloc[:0, :]) # horizontal empty
3183+
3184+
# FIXME: fix loc & xs
3185+
def test_loc_empty_list_indexer_is_ok(self):
3186+
raise nose.SkipTest('loc discards columns names')
3187+
from pandas.util.testing import makeCustomDataframe as mkdf
3188+
df = mkdf(5, 2)
3189+
assert_frame_equal(df.loc[:,[]], df.iloc[:, :0]) # vertical empty
3190+
assert_frame_equal(df.loc[[],:], df.iloc[:0, :]) # horizontal empty
3191+
3192+
def test_ix_empty_list_indexer_is_ok(self):
3193+
raise nose.SkipTest('ix discards columns names')
3194+
from pandas.util.testing import makeCustomDataframe as mkdf
3195+
df = mkdf(5, 2)
3196+
assert_frame_equal(df.ix[:,[]], df.iloc[:, :0]) # vertical empty
3197+
assert_frame_equal(df.ix[[],:], df.iloc[:0, :]) # horizontal empty
31783198

31793199
if __name__ == '__main__':
31803200
import nose

0 commit comments

Comments
 (0)