Skip to content

TST: More pytest.mark.parameterize #45106

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
Dec 29, 2021
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
37 changes: 19 additions & 18 deletions pandas/tests/arrays/sparse/test_libsparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,26 +393,27 @@ def test_lookup_array(self):
exp = np.array([-1, -1, 1, -1], dtype=np.int32)
tm.assert_numpy_array_equal(res, exp)

def test_lookup_basics(self):
def _check(index):
assert index.lookup(0) == -1
assert index.lookup(5) == 0
assert index.lookup(7) == 2
assert index.lookup(8) == -1
assert index.lookup(9) == -1
assert index.lookup(10) == -1
assert index.lookup(11) == -1
assert index.lookup(12) == 3
assert index.lookup(17) == 8
assert index.lookup(18) == -1

@pytest.mark.parametrize(
"idx, expected",
[
[0, -1],
[5, 0],
[7, 2],
[8, -1],
[9, -1],
[10, -1],
[11, -1],
[12, 3],
[17, 8],
[18, -1],
],
)
def test_lookup_basics(self, idx, expected):
bindex = BlockIndex(20, [5, 12], [3, 6])
iindex = bindex.to_int_index()
assert bindex.lookup(idx) == expected

_check(bindex)
_check(iindex)

# corner cases
iindex = bindex.to_int_index()
assert iindex.lookup(idx) == expected


class TestBlockIndex:
Expand Down
85 changes: 46 additions & 39 deletions pandas/tests/frame/test_stack_unstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1421,50 +1421,57 @@ def test_stack(self, multiindex_year_month_day_dataframe_random_data):
# stack with negative number
result = ymd.unstack(0).stack(-2)
expected = ymd.unstack(0).stack(0)
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
"idx, columns, exp_idx",
[
[
list("abab"),
["1st", "2nd", "3rd"],
MultiIndex(
levels=[["a", "b"], ["1st", "2nd", "3rd"]],
codes=[
np.tile(np.arange(2).repeat(3), 2),
np.tile(np.arange(3), 4),
],
),
],
[
list("abab"),
["1st", "2nd", "1st"],
MultiIndex(
levels=[["a", "b"], ["1st", "2nd"]],
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile([0, 1, 0], 4)],
),
],
[
MultiIndex.from_tuples((("a", 2), ("b", 1), ("a", 1), ("b", 2))),
["1st", "2nd", "1st"],
MultiIndex(
levels=[["a", "b"], [1, 2], ["1st", "2nd"]],
codes=[
np.tile(np.arange(2).repeat(3), 2),
np.repeat([1, 0, 1], [3, 6, 3]),
np.tile([0, 1, 0], 4),
],
),
],
],
)
def test_stack_duplicate_index(self, idx, columns, exp_idx):
# GH10417
def check(left, right):
tm.assert_series_equal(left, right)
assert left.index.is_unique is False
li, ri = left.index, right.index
tm.assert_index_equal(li, ri)

df = DataFrame(
np.arange(12).reshape(4, 3),
index=list("abab"),
columns=["1st", "2nd", "3rd"],
index=idx,
columns=columns,
)

mi = MultiIndex(
levels=[["a", "b"], ["1st", "2nd", "3rd"]],
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile(np.arange(3), 4)],
)

left, right = df.stack(), Series(np.arange(12), index=mi)
check(left, right)

df.columns = ["1st", "2nd", "1st"]
mi = MultiIndex(
levels=[["a", "b"], ["1st", "2nd"]],
codes=[np.tile(np.arange(2).repeat(3), 2), np.tile([0, 1, 0], 4)],
)

left, right = df.stack(), Series(np.arange(12), index=mi)
check(left, right)

tpls = ("a", 2), ("b", 1), ("a", 1), ("b", 2)
df.index = MultiIndex.from_tuples(tpls)
mi = MultiIndex(
levels=[["a", "b"], [1, 2], ["1st", "2nd"]],
codes=[
np.tile(np.arange(2).repeat(3), 2),
np.repeat([1, 0, 1], [3, 6, 3]),
np.tile([0, 1, 0], 4),
],
)

left, right = df.stack(), Series(np.arange(12), index=mi)
check(left, right)
result = df.stack()
expected = Series(np.arange(12), index=exp_idx)
tm.assert_series_equal(result, expected)
assert result.index.is_unique is False
li, ri = result.index, expected.index
tm.assert_index_equal(li, ri)

def test_unstack_odd_failure(self):
data = """day,time,smoker,sum,len
Expand Down
20 changes: 12 additions & 8 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,18 @@ def test_constructor(self):
int32block = create_block("i4", [0])
assert int32block.dtype == np.int32

def test_pickle(self):
def _check(blk):
assert_block_equal(tm.round_trip_pickle(blk), blk)

_check(self.fblock)
_check(self.cblock)
_check(self.oblock)
_check(self.bool_block)
@pytest.mark.parametrize(
"typ, data",
[
["float", [0, 2, 4]],
["complex", [7]],
["object", [1, 3]],
["bool", [5]],
],
)
def test_pickle(self, typ, data):
blk = create_block(typ, data)
assert_block_equal(tm.round_trip_pickle(blk), blk)

def test_mgr_locs(self):
assert isinstance(self.fblock.mgr_locs, BlockPlacement)
Expand Down
33 changes: 18 additions & 15 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,32 @@ def test_eng_float_formatter(self, float_frame):
repr(df)
tm.reset_display_options()

def test_show_null_counts(self):
@pytest.mark.parametrize(
"row, columns, show_counts, result",
[
[20, 20, None, True],
[20, 20, True, True],
[20, 20, False, False],
[5, 5, None, False],
[5, 5, True, False],
[5, 5, False, False],
],
)
def test_show_counts(self, row, columns, show_counts, result):

df = DataFrame(1, columns=range(10), index=range(10))
df.iloc[1, 1] = np.nan

def check(show_counts, result):
buf = StringIO()
df.info(buf=buf, show_counts=show_counts)
assert ("non-null" in buf.getvalue()) is result

with option_context(
"display.max_info_rows", 20, "display.max_info_columns", 20
"display.max_info_rows", row, "display.max_info_columns", columns
):
check(None, True)
check(True, True)
check(False, False)

with option_context("display.max_info_rows", 5, "display.max_info_columns", 5):
check(None, False)
check(True, False)
check(False, False)
with StringIO() as buf:
df.info(buf=buf, show_counts=show_counts)
assert ("non-null" in buf.getvalue()) is result

def test_show_null_counts_deprecation(self):
# GH37999
df = DataFrame(1, columns=range(10), index=range(10))
with tm.assert_produces_warning(
FutureWarning, match="null_counts is deprecated.+"
):
Expand Down
48 changes: 23 additions & 25 deletions pandas/tests/io/pytables/test_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,37 +219,35 @@ def test_put_mixed_type(setup_path):
tm.assert_frame_equal(expected, df)


def test_store_index_types(setup_path):
@pytest.mark.parametrize(
"format, index",
[
["table", tm.makeFloatIndex],
["table", tm.makeStringIndex],
["table", tm.makeIntIndex],
["table", tm.makeDateIndex],
["fixed", tm.makeFloatIndex],
["fixed", tm.makeStringIndex],
["fixed", tm.makeIntIndex],
["fixed", tm.makeDateIndex],
["table", tm.makePeriodIndex], # GH#7796
["fixed", tm.makePeriodIndex],
["table", tm.makeUnicodeIndex],
["fixed", tm.makeUnicodeIndex],
],
)
def test_store_index_types(setup_path, format, index):
# GH5386
# test storing various index types

with ensure_clean_store(setup_path) as store:

def check(format, index):
df = DataFrame(np.random.randn(10, 2), columns=list("AB"))
df.index = index(len(df))

_maybe_remove(store, "df")
store.put("df", df, format=format)
tm.assert_frame_equal(df, store["df"])

for index in [
tm.makeFloatIndex,
tm.makeStringIndex,
tm.makeIntIndex,
tm.makeDateIndex,
]:
df = DataFrame(np.random.randn(10, 2), columns=list("AB"))
df.index = index(len(df))

check("table", index)
check("fixed", index)

check("fixed", tm.makePeriodIndex)
check("table", tm.makePeriodIndex) # GH#7796

# unicode
index = tm.makeUnicodeIndex
check("table", index)
check("fixed", index)
_maybe_remove(store, "df")
store.put("df", df, format=format)
tm.assert_frame_equal(df, store["df"])


def test_column_multiindex(setup_path):
Expand Down