Skip to content

Commit

Permalink
TST: Fix makeIntIndex, benchmark get loc
Browse files Browse the repository at this point in the history
Author: Pietro Battiston <me@pietrobattiston.it>

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)
  • Loading branch information
toobaz authored and jreback committed Feb 5, 2018
1 parent 5b58a20 commit d5a7e7c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
17 changes: 17 additions & 0 deletions asv_bench/benchmarks/index_object.py
Expand Up @@ -147,6 +147,11 @@ def setup(self, dtype):
self.idx = getattr(tm, 'make{}Index'.format(dtype))(N)
self.array_mask = (np.arange(N) % 3) == 0
self.series_mask = Series(self.array_mask)
self.sorted = self.idx.sort_values()
half = N // 2
self.non_unique = self.idx[:half].append(self.idx[:half])
self.non_unique_sorted = self.sorted[:half].append(self.sorted[:half])
self.key = self.sorted[N // 4]

def time_boolean_array(self, dtype):
self.idx[self.array_mask]
Expand All @@ -163,6 +168,18 @@ def time_slice(self, dtype):
def time_slice_step(self, dtype):
self.idx[::2]

def time_get_loc(self, dtype):
self.idx.get_loc(self.key)

def time_get_loc_sorted(self, dtype):
self.sorted.get_loc(self.key)

def time_get_loc_non_unique(self, dtype):
self.non_unique.get_loc(self.key)

def time_get_loc_non_unique_sorted(self, dtype):
self.non_unique_sorted.get_loc(self.key)


class Float64IndexMethod(object):
# GH 13166
Expand Down
16 changes: 9 additions & 7 deletions pandas/tests/indexes/test_base.py
Expand Up @@ -830,15 +830,16 @@ def test_map_with_tuples(self):

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

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

# Test that returning a single object from a MultiIndex
# returns an Index.
Expand Down Expand Up @@ -870,7 +871,8 @@ def test_map_tseries_indices_return_index(self):
def test_map_dictlike(self, mapper):
# GH 12756
expected = Index(['foo', 'bar', 'baz'])
result = tm.makeIntIndex(3).map(mapper(expected.values, [0, 1, 2]))
idx = tm.makeIntIndex(3)
result = idx.map(mapper(expected.values, idx))
tm.assert_index_equal(result, expected)

for name in self.indices.keys():
Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/indexing/test_floats.py
Expand Up @@ -4,7 +4,8 @@

from warnings import catch_warnings
import numpy as np
from pandas import Series, DataFrame, Index, Float64Index
from pandas import (Series, DataFrame, Index, Float64Index, Int64Index,
RangeIndex)
from pandas.util.testing import assert_series_equal, assert_almost_equal
import pandas.util.testing as tm

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

# integer index
for index in [tm.makeIntIndex, tm.makeRangeIndex]:
for i in [Int64Index(range(5)), RangeIndex(5)]:

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

# s is an in-range index
s = Series(range(5), index=index)
Expand Down Expand Up @@ -486,9 +486,8 @@ def f():
def test_slice_integer_frame_getitem(self):

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

index = index(5)
s = DataFrame(np.random.randn(5, 2), index=index)

def f(idxr):
Expand Down

0 comments on commit d5a7e7c

Please sign in to comment.