Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 16 additions & 11 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,20 +666,25 @@ def test_float_comparison_bin_op(self, dtype):
res = df.eval("-5 > x")
assert res.values == np.array([False])

def test_disallow_scalar_bool_ops(self):
exprs = "1 or 2", "1 and 2"
exprs += "a and b", "a or b"
exprs += ("1 or 2 and (3 + 2) > 3",)
exprs += ("2 * x > 2 or 1 and 2",)
exprs += ("2 * df > 3 and 1 or a",)

@pytest.mark.parametrize(
"ex",
(
"1 or 2",
"1 and 2",
"a and b",
"a or b",
"1 or 2 and (3 + 2) > 3",
"2 * x > 2 or 1 and 2",
"2 * df > 3 and 1 or a",
),
)
def test_disallow_scalar_bool_ops(self, ex):
x, a, b = np.random.randn(3), 1, 2 # noqa:F841
df = DataFrame(np.random.randn(3, 2)) # noqa:F841

for ex in exprs:
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
with pytest.raises(NotImplementedError, match=msg):
pd.eval(ex, engine=self.engine, parser=self.parser)
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
with pytest.raises(NotImplementedError, match=msg):
pd.eval(ex, engine=self.engine, parser=self.parser)

def test_identical(self):
# see gh-10546
Expand Down
52 changes: 26 additions & 26 deletions pandas/tests/libs/test_hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,19 @@ def test_get_state(self, table_type, dtype):
assert "n_buckets" in state
assert "upper_bound" in state

def test_no_reallocation(self, table_type, dtype):
for N in range(1, 110):
keys = np.arange(N).astype(dtype)
preallocated_table = table_type(N)
n_buckets_start = preallocated_table.get_state()["n_buckets"]
preallocated_table.map_locations(keys)
n_buckets_end = preallocated_table.get_state()["n_buckets"]
# original number of buckets was enough:
assert n_buckets_start == n_buckets_end
# check with clean table (not too much preallocated)
clean_table = table_type()
clean_table.map_locations(keys)
assert n_buckets_start == clean_table.get_state()["n_buckets"]
@pytest.mark.parametrize("N", range(1, 110))
def test_no_reallocation(self, table_type, dtype, N):
keys = np.arange(N).astype(dtype)
preallocated_table = table_type(N)
n_buckets_start = preallocated_table.get_state()["n_buckets"]
preallocated_table.map_locations(keys)
n_buckets_end = preallocated_table.get_state()["n_buckets"]
# original number of buckets was enough:
assert n_buckets_start == n_buckets_end
# check with clean table (not too much preallocated)
clean_table = table_type()
clean_table.map_locations(keys)
assert n_buckets_start == clean_table.get_state()["n_buckets"]


class TestPyObjectHashTableWithNans:
Expand Down Expand Up @@ -282,19 +282,19 @@ def test_tracemalloc_for_empty_StringHashTable():
assert get_allocated_khash_memory() == 0


def test_no_reallocation_StringHashTable():
for N in range(1, 110):
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
preallocated_table = ht.StringHashTable(N)
n_buckets_start = preallocated_table.get_state()["n_buckets"]
preallocated_table.map_locations(keys)
n_buckets_end = preallocated_table.get_state()["n_buckets"]
# original number of buckets was enough:
assert n_buckets_start == n_buckets_end
# check with clean table (not too much preallocated)
clean_table = ht.StringHashTable()
clean_table.map_locations(keys)
assert n_buckets_start == clean_table.get_state()["n_buckets"]
@pytest.mark.parametrize("N", range(1, 110))
def test_no_reallocation_StringHashTable(N):
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
preallocated_table = ht.StringHashTable(N)
n_buckets_start = preallocated_table.get_state()["n_buckets"]
preallocated_table.map_locations(keys)
n_buckets_end = preallocated_table.get_state()["n_buckets"]
# original number of buckets was enough:
assert n_buckets_start == n_buckets_end
# check with clean table (not too much preallocated)
clean_table = ht.StringHashTable()
clean_table.map_locations(keys)
assert n_buckets_start == clean_table.get_state()["n_buckets"]


@pytest.mark.parametrize(
Expand Down
162 changes: 90 additions & 72 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,58 @@ def test_maybe_indices_to_slice_left_edge(self):
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

for end in [1, 2, 5, 20, 99]:
for step in [1, 2, 4]:
indices = np.arange(0, end, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
@pytest.mark.parametrize("end", [1, 2, 5, 20, 99])
@pytest.mark.parametrize("step", [1, 2, 4])
def test_maybe_indices_to_slice_left_edge_not_slice_end_steps(self, end, step):
target = np.arange(100)
indices = np.arange(0, end, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

@pytest.mark.parametrize(
"case", [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]
)
def test_maybe_indices_to_slice_left_edge_not_slice(self, case):
# not slice
for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]:
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
target = np.arange(100)
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

def test_maybe_indices_to_slice_right_edge(self):
@pytest.mark.parametrize("start", [0, 2, 5, 20, 97, 98])
@pytest.mark.parametrize("step", [1, 2, 4])
def test_maybe_indices_to_slice_right_edge(self, start, step):
target = np.arange(100)

# slice
for start in [0, 2, 5, 20, 97, 98]:
for step in [1, 2, 4]:
indices = np.arange(start, 99, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
indices = np.arange(start, 99, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

def test_maybe_indices_to_slice_right_edge_not_slice(self):
# not slice
target = np.arange(100)
indices = np.array([97, 98, 99, 100], dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

Expand All @@ -122,65 +130,75 @@ def test_maybe_indices_to_slice_right_edge(self):
with pytest.raises(IndexError, match=msg):
target[maybe_slice]

for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
@pytest.mark.parametrize(
"case", [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]
)
def test_maybe_indices_to_slice_right_edge_cases(self, case):
target = np.arange(100)
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

def test_maybe_indices_to_slice_both_edges(self):
@pytest.mark.parametrize("step", [1, 2, 4, 5, 8, 9])
def test_maybe_indices_to_slice_both_edges(self, step):
target = np.arange(10)

# slice
for step in [1, 2, 4, 5, 8, 9]:
indices = np.arange(0, 9, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
indices = np.arange(0, 9, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

@pytest.mark.parametrize("case", [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]])
def test_maybe_indices_to_slice_both_edges_not_slice(self, case):
# not slice
for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

def test_maybe_indices_to_slice_middle(self):
target = np.arange(10)
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

@pytest.mark.parametrize("start, end", [(2, 10), (5, 25), (65, 97)])
@pytest.mark.parametrize("step", [1, 2, 4, 20])
def test_maybe_indices_to_slice_middle(self, start, end, step):
target = np.arange(100)

# slice
for start, end in [(2, 10), (5, 25), (65, 97)]:
for step in [1, 2, 4, 20]:
indices = np.arange(start, end, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
indices = np.arange(start, end, step, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
# reverse
indices = indices[::-1]
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

@pytest.mark.parametrize(
"case", [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]
)
def test_maybe_indices_to_slice_middle_not_slice(self, case):
# not slice
for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
target = np.arange(100)
indices = np.array(case, dtype=np.intp)
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))

assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
assert not isinstance(maybe_slice, slice)
tm.assert_numpy_array_equal(maybe_slice, indices)
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])

def test_maybe_booleans_to_slice(self):
arr = np.array([0, 0, 1, 1, 1, 0, 1], dtype=np.uint8)
Expand Down
Loading