Skip to content

Commit aacbaa8

Browse files
PERF: improve iloc list indexing
1 parent 1f89060 commit aacbaa8

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

pandas/core/indexing.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -1697,34 +1697,38 @@ def _get_slice_axis(self, slice_obj, axis=0):
16971697
else:
16981698
return self.obj.take(slice_obj, axis=axis, convert=False)
16991699

1700-
def _get_list_axis(self, key_list, axis=0):
1700+
def _get_list_axis(self, key, axis=0):
17011701
"""
1702-
Return Series values by list or array of integers
1702+
Return Series values by array of integers
17031703
17041704
Parameters
17051705
----------
1706-
key_list : list-like positional indexer
1706+
key : list-like positional indexer (already converted to array)
17071707
axis : int (can only be zero)
17081708
17091709
Returns
17101710
-------
17111711
Series object
17121712
"""
1713-
1714-
# validate list bounds
1715-
self._is_valid_list_like(key_list, axis)
1716-
1717-
# force an actual list
1718-
key_list = list(key_list)
1719-
return self.obj.take(key_list, axis=axis, convert=False)
1713+
try:
1714+
return self.obj.take(key, axis=axis, convert=False)
1715+
except IndexError:
1716+
# re-raise with different error message
1717+
raise IndexError("positional indexers are out-of-bounds")
17201718

17211719
def _getitem_axis(self, key, axis=0):
17221720

17231721
if isinstance(key, slice):
17241722
self._has_valid_type(key, axis)
17251723
return self._get_slice_axis(key, axis=axis)
17261724

1727-
elif is_bool_indexer(key):
1725+
if isinstance(key, list):
1726+
try:
1727+
key = np.asarray(key)
1728+
except TypeError: # pragma: no cover
1729+
pass
1730+
1731+
if is_bool_indexer(key):
17281732
self._has_valid_type(key, axis)
17291733
return self._getbool_axis(key, axis=axis)
17301734

@@ -1734,6 +1738,7 @@ def _getitem_axis(self, key, axis=0):
17341738

17351739
# a single integer
17361740
else:
1741+
17371742
key = self._convert_scalar_indexer(key, axis)
17381743

17391744
if not is_integer(key):

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2378,7 +2378,7 @@ def take(self, indices, axis=0, convert=True, is_copy=False, **kwargs):
23782378
--------
23792379
numpy.ndarray.take
23802380
"""
2381-
nv.validate_take(tuple(), kwargs)
2381+
#nv.validate_take(tuple(), kwargs)
23822382

23832383
# check/convert indicies here
23842384
if convert:
@@ -2388,7 +2388,7 @@ def take(self, indices, axis=0, convert=True, is_copy=False, **kwargs):
23882388
new_index = self.index.take(indices)
23892389
new_values = self._values.take(indices)
23902390
return self._constructor(new_values,
2391-
index=new_index).__finalize__(self)
2391+
index=new_index, fastpath=True).__finalize__(self)
23922392

23932393
def isin(self, values):
23942394
"""

pandas/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ def _append_same_dtype(self, to_concat, name):
16681668
@Appender(_index_shared_docs['take'] % _index_doc_kwargs)
16691669
def take(self, indices, axis=0, allow_fill=True,
16701670
fill_value=None, **kwargs):
1671-
nv.validate_take(tuple(), kwargs)
1671+
#nv.validate_take(tuple(), kwargs)
16721672
indices = _ensure_platform_int(indices)
16731673
if self._can_hold_na:
16741674
taken = self._assert_take_fillable(self.values, indices,

0 commit comments

Comments
 (0)