Skip to content

Commit 181fea4

Browse files
toobazharisbal
authored and
harisbal
committed
TST: Fix makeIntIndex, benchmark get loc
Author: Pietro Battiston <[email protected]> Closes pandas-dev#19483 from toobaz/test_get_loc and squashes the following commits: 51d6911 [Pietro Battiston] TST: benchmark get_loc in various cases d424f63 [Pietro Battiston] TST: produce unsorted integer index (consistently with other types)
1 parent 4ee165c commit 181fea4

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

asv_bench/benchmarks/index_object.py

+17
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ def setup(self, dtype):
147147
self.idx = getattr(tm, 'make{}Index'.format(dtype))(N)
148148
self.array_mask = (np.arange(N) % 3) == 0
149149
self.series_mask = Series(self.array_mask)
150+
self.sorted = self.idx.sort_values()
151+
half = N // 2
152+
self.non_unique = self.idx[:half].append(self.idx[:half])
153+
self.non_unique_sorted = self.sorted[:half].append(self.sorted[:half])
154+
self.key = self.sorted[N // 4]
150155

151156
def time_boolean_array(self, dtype):
152157
self.idx[self.array_mask]
@@ -163,6 +168,18 @@ def time_slice(self, dtype):
163168
def time_slice_step(self, dtype):
164169
self.idx[::2]
165170

171+
def time_get_loc(self, dtype):
172+
self.idx.get_loc(self.key)
173+
174+
def time_get_loc_sorted(self, dtype):
175+
self.sorted.get_loc(self.key)
176+
177+
def time_get_loc_non_unique(self, dtype):
178+
self.non_unique.get_loc(self.key)
179+
180+
def time_get_loc_non_unique_sorted(self, dtype):
181+
self.non_unique_sorted.get_loc(self.key)
182+
166183

167184
class Float64IndexMethod(object):
168185
# GH 13166

pandas/tests/indexes/test_base.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,16 @@ def test_map_with_tuples(self):
830830

831831
# Test that returning a single tuple from an Index
832832
# returns an Index.
833-
boolean_index = tm.makeIntIndex(3).map(lambda x: (x,))
834-
expected = Index([(0,), (1,), (2,)])
835-
tm.assert_index_equal(boolean_index, expected)
833+
idx = tm.makeIntIndex(3)
834+
result = tm.makeIntIndex(3).map(lambda x: (x,))
835+
expected = Index([(i,) for i in idx])
836+
tm.assert_index_equal(result, expected)
836837

837838
# Test that returning a tuple from a map of a single index
838839
# returns a MultiIndex object.
839-
boolean_index = tm.makeIntIndex(3).map(lambda x: (x, x == 1))
840-
expected = MultiIndex.from_tuples([(0, False), (1, True), (2, False)])
841-
tm.assert_index_equal(boolean_index, expected)
840+
result = idx.map(lambda x: (x, x == 1))
841+
expected = MultiIndex.from_tuples([(i, i == 1) for i in idx])
842+
tm.assert_index_equal(result, expected)
842843

843844
# Test that returning a single object from a MultiIndex
844845
# returns an Index.
@@ -870,7 +871,8 @@ def test_map_tseries_indices_return_index(self):
870871
def test_map_dictlike(self, mapper):
871872
# GH 12756
872873
expected = Index(['foo', 'bar', 'baz'])
873-
result = tm.makeIntIndex(3).map(mapper(expected.values, [0, 1, 2]))
874+
idx = tm.makeIntIndex(3)
875+
result = idx.map(mapper(expected.values, idx))
874876
tm.assert_index_equal(result, expected)
875877

876878
for name in self.indices.keys():

pandas/tests/indexing/test_floats.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from warnings import catch_warnings
66
import numpy as np
7-
from pandas import Series, DataFrame, Index, Float64Index
7+
from pandas import (Series, DataFrame, Index, Float64Index, Int64Index,
8+
RangeIndex)
89
from pandas.util.testing import assert_series_equal, assert_almost_equal
910
import pandas.util.testing as tm
1011

@@ -206,9 +207,8 @@ def test_scalar_integer(self):
206207
# test how scalar float indexers work on int indexes
207208

208209
# integer index
209-
for index in [tm.makeIntIndex, tm.makeRangeIndex]:
210+
for i in [Int64Index(range(5)), RangeIndex(5)]:
210211

211-
i = index(5)
212212
for s in [Series(np.arange(len(i))),
213213
DataFrame(np.random.randn(len(i), len(i)),
214214
index=i, columns=i)]:
@@ -362,9 +362,9 @@ def test_slice_integer(self):
362362
# these coerce to a like integer
363363
# oob indicates if we are out of bounds
364364
# of positional indexing
365-
for index, oob in [(tm.makeIntIndex(5), False),
366-
(tm.makeRangeIndex(5), False),
367-
(tm.makeIntIndex(5) + 10, True)]:
365+
for index, oob in [(Int64Index(range(5)), False),
366+
(RangeIndex(5), False),
367+
(Int64Index(range(5)) + 10, True)]:
368368

369369
# s is an in-range index
370370
s = Series(range(5), index=index)
@@ -486,9 +486,8 @@ def f():
486486
def test_slice_integer_frame_getitem(self):
487487

488488
# similar to above, but on the getitem dim (of a DataFrame)
489-
for index in [tm.makeIntIndex, tm.makeRangeIndex]:
489+
for index in [Int64Index(range(5)), RangeIndex(5)]:
490490

491-
index = index(5)
492491
s = DataFrame(np.random.randn(5, 2), index=index)
493492

494493
def f(idxr):

0 commit comments

Comments
 (0)