diff --git a/pandas/tests/arrays/sparse/test_libsparse.py b/pandas/tests/arrays/sparse/test_libsparse.py index db63bba4d4eaf..527ae3ce37d1e 100644 --- a/pandas/tests/arrays/sparse/test_libsparse.py +++ b/pandas/tests/arrays/sparse/test_libsparse.py @@ -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: diff --git a/pandas/tests/frame/test_stack_unstack.py b/pandas/tests/frame/test_stack_unstack.py index 4b3ddbc6c193c..7efe4b08895bc 100644 --- a/pandas/tests/frame/test_stack_unstack.py +++ b/pandas/tests/frame/test_stack_unstack.py @@ -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 diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index ef313b2840107..a5ba684a37edf 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -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) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index fe6914e6ef4f5..bf0a10fa702a5 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -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.+" ): diff --git a/pandas/tests/io/pytables/test_put.py b/pandas/tests/io/pytables/test_put.py index 41addc5023436..8e7b31bcf8bca 100644 --- a/pandas/tests/io/pytables/test_put.py +++ b/pandas/tests/io/pytables/test_put.py @@ -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):