Skip to content

Commit ab20769

Browse files
committed
Merge pull request #9127 from shoyer/fix-slow-loc
PERF: fix slow s.loc[[0]]
2 parents 482af4c + 3979dc7 commit ab20769

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Performance
4545

4646
.. _whatsnew_0160.performance:
4747

48+
- Fixed a severe performance regression for ``.loc`` indexing with an array or list (:issue:9126:).
4849

4950
Bug Fixes
5051
~~~~~~~~~

pandas/core/indexing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1275,9 +1275,8 @@ def _has_valid_type(self, key, axis):
12751275
if isinstance(key, tuple) and isinstance(ax, MultiIndex):
12761276
return True
12771277

1278-
# require at least 1 element in the index
1279-
idx = _ensure_index(key)
1280-
if len(idx) and not idx.isin(ax).any():
1278+
# TODO: don't check the entire key unless necessary
1279+
if len(key) and np.all(ax.get_indexer_for(key) < 0):
12811280

12821281
raise KeyError("None of [%s] are in the [%s]" %
12831282
(key, self.obj._get_axis_name(axis)))

vb_suite/indexing.py

+27
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,30 @@
209209

210210
frame_iloc_big = Benchmark('df.iloc[:100,0]', setup,
211211
start_date=datetime(2013, 1, 1))
212+
213+
#----------------------------------------------------------------------
214+
# basic tests for [], .loc[], .iloc[] and .ix[]
215+
216+
setup = common_setup + """
217+
s = Series(np.random.rand(1000000))
218+
"""
219+
220+
series_getitem_scalar = Benchmark("s[800000]", setup)
221+
series_getitem_slice = Benchmark("s[:800000]", setup)
222+
series_getitem_list_like = Benchmark("s[[800000]]", setup)
223+
series_getitem_array = Benchmark("s[np.arange(10000)]", setup)
224+
225+
series_loc_scalar = Benchmark("s.loc[800000]", setup)
226+
series_loc_slice = Benchmark("s.loc[:800000]", setup)
227+
series_loc_list_like = Benchmark("s.loc[[800000]]", setup)
228+
series_loc_array = Benchmark("s.loc[np.arange(10000)]", setup)
229+
230+
series_iloc_scalar = Benchmark("s.loc[800000]", setup)
231+
series_iloc_slice = Benchmark("s.loc[:800000]", setup)
232+
series_iloc_list_like = Benchmark("s.loc[[800000]]", setup)
233+
series_iloc_array = Benchmark("s.loc[np.arange(10000)]", setup)
234+
235+
series_ix_scalar = Benchmark("s.ix[800000]", setup)
236+
series_ix_slice = Benchmark("s.ix[:800000]", setup)
237+
series_ix_list_like = Benchmark("s.ix[[800000]]", setup)
238+
series_ix_array = Benchmark("s.ix[np.arange(10000)]", setup)

0 commit comments

Comments
 (0)