Skip to content

CLN: no need for suffices anymore in test_hashtable.py #44034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 46 additions & 62 deletions pandas/tests/libs/test_hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,96 +370,85 @@ def test_unique_for_nan_objects_tuple():
assert len(unique) == 2


def get_ht_function(fun_name, type_suffix):
return getattr(ht, fun_name)


@pytest.mark.parametrize(
"dtype, type_suffix",
"dtype",
[
(np.object_, "object"),
(np.complex128, "complex128"),
(np.int64, "int64"),
(np.uint64, "uint64"),
(np.float64, "float64"),
(np.complex64, "complex64"),
(np.int32, "int32"),
(np.uint32, "uint32"),
(np.float32, "float32"),
(np.int16, "int16"),
(np.uint16, "uint16"),
(np.int8, "int8"),
(np.uint8, "uint8"),
(np.intp, "intp"),
np.object_,
np.complex128,
np.int64,
np.uint64,
np.float64,
np.complex64,
np.int32,
np.uint32,
np.float32,
np.int16,
np.uint16,
np.int8,
np.uint8,
np.intp,
],
)
class TestHelpFunctions:
def test_value_count(self, dtype, type_suffix, writable):
def test_value_count(self, dtype, writable):
N = 43
value_count = get_ht_function("value_count", type_suffix)
expected = (np.arange(N) + N).astype(dtype)
values = np.repeat(expected, 5)
values.flags.writeable = writable
keys, counts = value_count(values, False)
keys, counts = ht.value_count(values, False)
tm.assert_numpy_array_equal(np.sort(keys), expected)
assert np.all(counts == 5)

def test_value_count_stable(self, dtype, type_suffix, writable):
def test_value_count_stable(self, dtype, writable):
# GH12679
value_count = get_ht_function("value_count", type_suffix)
values = np.array([2, 1, 5, 22, 3, -1, 8]).astype(dtype)
values.flags.writeable = writable
keys, counts = value_count(values, False)
keys, counts = ht.value_count(values, False)
tm.assert_numpy_array_equal(keys, values)
assert np.all(counts == 1)

def test_duplicated_first(self, dtype, type_suffix, writable):
def test_duplicated_first(self, dtype, writable):
N = 100
duplicated = get_ht_function("duplicated", type_suffix)
values = np.repeat(np.arange(N).astype(dtype), 5)
values.flags.writeable = writable
result = duplicated(values)
result = ht.duplicated(values)
expected = np.ones_like(values, dtype=np.bool_)
expected[::5] = False
tm.assert_numpy_array_equal(result, expected)

def test_ismember_yes(self, dtype, type_suffix, writable):
def test_ismember_yes(self, dtype, writable):
N = 127
ismember = get_ht_function("ismember", type_suffix)
arr = np.arange(N).astype(dtype)
values = np.arange(N).astype(dtype)
arr.flags.writeable = writable
values.flags.writeable = writable
result = ismember(arr, values)
result = ht.ismember(arr, values)
expected = np.ones_like(values, dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

def test_ismember_no(self, dtype, type_suffix):
def test_ismember_no(self, dtype):
N = 17
ismember = get_ht_function("ismember", type_suffix)
arr = np.arange(N).astype(dtype)
values = (np.arange(N) + N).astype(dtype)
result = ismember(arr, values)
result = ht.ismember(arr, values)
expected = np.zeros_like(values, dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

def test_mode(self, dtype, type_suffix, writable):
def test_mode(self, dtype, writable):
if dtype in (np.int8, np.uint8):
N = 53
else:
N = 11111
mode = get_ht_function("mode", type_suffix)
values = np.repeat(np.arange(N).astype(dtype), 5)
values[0] = 42
values.flags.writeable = writable
result = mode(values, False)
result = ht.mode(values, False)
assert result == 42

def test_mode_stable(self, dtype, type_suffix, writable):
mode = get_ht_function("mode", type_suffix)
def test_mode_stable(self, dtype, writable):
values = np.array([2, 1, 5, 22, 3, -1, 8]).astype(dtype)
values.flags.writeable = writable
keys = mode(values, False)
keys = ht.mode(values, False)
tm.assert_numpy_array_equal(keys, values)


Expand All @@ -482,52 +471,47 @@ def test_unique_label_indices_intp(writable):


@pytest.mark.parametrize(
"dtype, type_suffix",
"dtype",
[
(np.float64, "float64"),
(np.float32, "float32"),
(np.complex128, "complex128"),
(np.complex64, "complex64"),
np.float64,
np.float32,
np.complex128,
np.complex64,
],
)
class TestHelpFunctionsWithNans:
def test_value_count(self, dtype, type_suffix):
value_count = get_ht_function("value_count", type_suffix)
def test_value_count(self, dtype):
values = np.array([np.nan, np.nan, np.nan], dtype=dtype)
keys, counts = value_count(values, True)
keys, counts = ht.value_count(values, True)
assert len(keys) == 0
keys, counts = value_count(values, False)
keys, counts = ht.value_count(values, False)
assert len(keys) == 1 and np.all(np.isnan(keys))
assert counts[0] == 3

def test_duplicated_first(self, dtype, type_suffix):
duplicated = get_ht_function("duplicated", type_suffix)
def test_duplicated_first(self, dtype):
values = np.array([np.nan, np.nan, np.nan], dtype=dtype)
result = duplicated(values)
result = ht.duplicated(values)
expected = np.array([False, True, True])
tm.assert_numpy_array_equal(result, expected)

def test_ismember_yes(self, dtype, type_suffix):
ismember = get_ht_function("ismember", type_suffix)
def test_ismember_yes(self, dtype):
arr = np.array([np.nan, np.nan, np.nan], dtype=dtype)
values = np.array([np.nan, np.nan], dtype=dtype)
result = ismember(arr, values)
result = ht.ismember(arr, values)
expected = np.array([True, True, True], dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

def test_ismember_no(self, dtype, type_suffix):
ismember = get_ht_function("ismember", type_suffix)
def test_ismember_no(self, dtype):
arr = np.array([np.nan, np.nan, np.nan], dtype=dtype)
values = np.array([1], dtype=dtype)
result = ismember(arr, values)
result = ht.ismember(arr, values)
expected = np.array([False, False, False], dtype=np.bool_)
tm.assert_numpy_array_equal(result, expected)

def test_mode(self, dtype, type_suffix):
mode = get_ht_function("mode", type_suffix)
def test_mode(self, dtype):
values = np.array([42, np.nan, np.nan, np.nan], dtype=dtype)
assert mode(values, True) == 42
assert np.isnan(mode(values, False))
assert ht.mode(values, True) == 42
assert np.isnan(ht.mode(values, False))


def test_ismember_tuple_with_nans():
Expand Down