From 1b88d3299222e94f50760290442881690aaa04d7 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 30 Nov 2021 20:59:59 -0800 Subject: [PATCH] TST: de-duplicate fixtures --- pandas/conftest.py | 14 ++++-- pandas/tests/arithmetic/conftest.py | 20 +++------ pandas/tests/frame/methods/test_drop.py | 13 +----- pandas/tests/frame/methods/test_sort_index.py | 40 +++-------------- pandas/tests/groupby/conftest.py | 14 ++---- pandas/tests/groupby/test_allowlist.py | 29 +++--------- pandas/tests/indexes/multi/test_monotonic.py | 20 ++++----- pandas/tests/indexes/test_common.py | 13 +++--- pandas/tests/io/formats/test_series_info.py | 14 +++--- pandas/tests/io/pytables/test_append.py | 11 ++--- pandas/tests/io/pytables/test_round_trip.py | 10 +---- pandas/tests/reshape/concat/test_index.py | 14 ++---- pandas/tests/reshape/concat/test_series.py | 10 ++--- pandas/tests/reshape/merge/test_join.py | 8 +--- pandas/tests/reshape/merge/test_multi.py | 44 +++---------------- pandas/tests/series/indexing/test_indexing.py | 9 +--- pandas/tests/series/test_repr.py | 13 ++---- 17 files changed, 76 insertions(+), 220 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 04589993b5f53..eb9a952250f36 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -440,13 +440,21 @@ def multiindex_year_month_day_dataframe_random_data(): @pytest.fixture -def multiindex_dataframe_random_data(): - """DataFrame with 2 level MultiIndex with random data""" - index = MultiIndex( +def lexsorted_two_level_string_multiindex(): + """ + 2-level MultiIndex, lexsorted, with string names. + """ + return MultiIndex( levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], names=["first", "second"], ) + + +@pytest.fixture +def multiindex_dataframe_random_data(lexsorted_two_level_string_multiindex): + """DataFrame with 2 level MultiIndex with random data""" + index = lexsorted_two_level_string_multiindex return DataFrame( np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp") ) diff --git a/pandas/tests/arithmetic/conftest.py b/pandas/tests/arithmetic/conftest.py index edab089745cb7..55cbfaf76d5a7 100644 --- a/pandas/tests/arithmetic/conftest.py +++ b/pandas/tests/arithmetic/conftest.py @@ -22,18 +22,6 @@ def switch_numexpr_min_elements(request): expr._MIN_ELEMENTS = _MIN_ELEMENTS -# ------------------------------------------------------------------ -# Helper Functions - - -def id_func(x): - if isinstance(x, tuple): - assert len(x) == 2 - return x[0].__name__ + "-" + str(x[1]) - else: - return x.__name__ - - # ------------------------------------------------------------------ @@ -228,7 +216,9 @@ def mismatched_freq(request): # ------------------------------------------------------------------ -@pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, pd.array], ids=id_func) +@pytest.fixture( + params=[pd.Index, pd.Series, pd.DataFrame, pd.array], ids=lambda x: x.__name__ +) def box_with_array(request): """ Fixture to test behavior for Index, Series, DataFrame, and pandas Array @@ -237,7 +227,9 @@ def box_with_array(request): return request.param -@pytest.fixture(params=[pd.Index, pd.Series, tm.to_array, np.array, list], ids=id_func) +@pytest.fixture( + params=[pd.Index, pd.Series, tm.to_array, np.array, list], ids=lambda x: x.__name__ +) def box_1d_array(request): """ Fixture to test behavior for Index, Series, tm.to_array, numpy Array and list diff --git a/pandas/tests/frame/methods/test_drop.py b/pandas/tests/frame/methods/test_drop.py index 1e2ce38a2fefd..4f8ea6eda1b5f 100644 --- a/pandas/tests/frame/methods/test_drop.py +++ b/pandas/tests/frame/methods/test_drop.py @@ -381,17 +381,8 @@ def test_drop_nonunique(self): tm.assert_frame_equal(result, expected) - def test_drop_level(self): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - frame = DataFrame( - np.random.randn(10, 3), - index=index, - columns=Index(["A", "B", "C"], name="exp"), - ) + def test_drop_level(self, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data result = frame.drop(["bar", "qux"], level="first") expected = frame.iloc[[0, 1, 2, 5, 6]] diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 1556a9507ab9e..99ff0f04afd60 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -6,7 +6,6 @@ CategoricalDtype, CategoricalIndex, DataFrame, - Index, IntervalIndex, MultiIndex, RangeIndex, @@ -591,17 +590,8 @@ def test_sort_index_and_reconstruction(self): assert result.columns.is_monotonic # TODO: better name, de-duplicate with test_sort_index_level above - def test_sort_index_level2(self): - mi = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - frame = DataFrame( - np.random.randn(10, 3), - index=mi, - columns=Index(["A", "B", "C"], name="exp"), - ) + def test_sort_index_level2(self, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data df = frame.copy() df.index = np.arange(len(df)) @@ -639,34 +629,16 @@ def test_sort_index_level_large_cardinality(self): assert (result.dtypes.values == df.dtypes.values).all() assert result.index._lexsort_depth == 3 - def test_sort_index_level_by_name(self): - mi = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - frame = DataFrame( - np.random.randn(10, 3), - index=mi, - columns=Index(["A", "B", "C"], name="exp"), - ) + def test_sort_index_level_by_name(self, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data frame.index.names = ["first", "second"] result = frame.sort_index(level="second") expected = frame.sort_index(level=1) tm.assert_frame_equal(result, expected) - def test_sort_index_level_mixed(self): - mi = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - frame = DataFrame( - np.random.randn(10, 3), - index=mi, - columns=Index(["A", "B", "C"], name="exp"), - ) + def test_sort_index_level_mixed(self, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data sorted_before = frame.sort_index(level=1) diff --git a/pandas/tests/groupby/conftest.py b/pandas/tests/groupby/conftest.py index 283342e71bb09..b61f6700f8d1d 100644 --- a/pandas/tests/groupby/conftest.py +++ b/pandas/tests/groupby/conftest.py @@ -1,10 +1,7 @@ import numpy as np import pytest -from pandas import ( - DataFrame, - MultiIndex, -) +from pandas import DataFrame import pandas._testing as tm from pandas.core.groupby.base import ( reduction_kernels, @@ -23,13 +20,8 @@ def as_index(request): @pytest.fixture -def mframe(): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - return DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) +def mframe(multiindex_dataframe_random_data): + return multiindex_dataframe_random_data @pytest.fixture diff --git a/pandas/tests/groupby/test_allowlist.py b/pandas/tests/groupby/test_allowlist.py index aa7229f1ab7b3..9d9a2e39e06c7 100644 --- a/pandas/tests/groupby/test_allowlist.py +++ b/pandas/tests/groupby/test_allowlist.py @@ -10,8 +10,6 @@ from pandas import ( DataFrame, - Index, - MultiIndex, Series, date_range, ) @@ -89,16 +87,6 @@ def s_allowlist_fixture(request): return request.param -@pytest.fixture -def mframe(): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - return DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) - - @pytest.fixture def df(): return DataFrame( @@ -174,18 +162,11 @@ def test_groupby_frame_allowlist(df_letters, df_allowlist_fixture): @pytest.fixture -def raw_frame(): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - raw_frame = DataFrame( - np.random.randn(10, 3), index=index, columns=Index(["A", "B", "C"], name="exp") - ) - raw_frame.iloc[1, [1, 2]] = np.nan - raw_frame.iloc[7, [0, 1]] = np.nan - return raw_frame +def raw_frame(multiindex_dataframe_random_data): + df = multiindex_dataframe_random_data + df.iloc[1, [1, 2]] = np.nan + df.iloc[7, [0, 1]] = np.nan + return df @pytest.mark.parametrize("op", AGG_FUNCTIONS) diff --git a/pandas/tests/indexes/multi/test_monotonic.py b/pandas/tests/indexes/multi/test_monotonic.py index b31e50330d3cd..74be9f829cb68 100644 --- a/pandas/tests/indexes/multi/test_monotonic.py +++ b/pandas/tests/indexes/multi/test_monotonic.py @@ -7,6 +7,15 @@ ) +def test_is_monotonic_increasing_lexsorted(lexsorted_two_level_string_multiindex): + # string ordering + mi = lexsorted_two_level_string_multiindex + assert mi.is_monotonic is False + assert Index(mi.values).is_monotonic is False + assert mi._is_strictly_monotonic_increasing is False + assert Index(mi.values)._is_strictly_monotonic_increasing is False + + def test_is_monotonic_increasing(): i = MultiIndex.from_product([np.arange(10), np.arange(10)], names=["one", "two"]) assert i.is_monotonic is True @@ -36,17 +45,6 @@ def test_is_monotonic_increasing(): assert Index(i.values).is_monotonic is False assert Index(i.values)._is_strictly_monotonic_increasing is False - # string ordering - i = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - assert i.is_monotonic is False - assert Index(i.values).is_monotonic is False - assert i._is_strictly_monotonic_increasing is False - assert Index(i.values)._is_strictly_monotonic_increasing is False - i = MultiIndex( levels=[["bar", "baz", "foo", "qux"], ["mom", "next", "zenith"]], codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py index 80edaf77fe960..fff11583e5161 100644 --- a/pandas/tests/indexes/test_common.py +++ b/pandas/tests/indexes/test_common.py @@ -49,15 +49,14 @@ def test_to_frame(self, name, index_flat): df = idx.to_frame(index=False, name=idx_name) assert df.index is not idx - def test_droplevel(self, index): + def test_droplevel(self, index_flat): # GH 21115 - if isinstance(index, MultiIndex): - # Tested separately in test_multi.py - return + # MultiIndex is tested separately in test_multi.py + index = index_flat assert index.droplevel([]).equals(index) - for level in index.name, [index.name]: + for level in [index.name, [index.name]]: if isinstance(index.name, tuple) and level is index.name: # GH 21121 : droplevel with tuple name continue @@ -174,8 +173,6 @@ def test_copy_name(self, index_flat): def test_copy_name2(self, index_flat): # GH#35592 index = index_flat - if isinstance(index, MultiIndex): - return assert index.copy(name="mario").name == "mario" @@ -192,7 +189,7 @@ def test_unique_level(self, index_flat): # GH 17896 expected = index.drop_duplicates() - for level in 0, index.name, None: + for level in [0, index.name, None]: result = index.unique(level=level) tm.assert_index_equal(result, expected) diff --git a/pandas/tests/io/formats/test_series_info.py b/pandas/tests/io/formats/test_series_info.py index 1f755d675d078..fc83b6c3d23ea 100644 --- a/pandas/tests/io/formats/test_series_info.py +++ b/pandas/tests/io/formats/test_series_info.py @@ -37,15 +37,11 @@ def test_info_categorical(): @pytest.mark.parametrize("verbose", [True, False]) -def test_info_series(verbose): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - s = Series(range(len(index)), index=index, name="sth") +def test_info_series(lexsorted_two_level_string_multiindex, verbose): + index = lexsorted_two_level_string_multiindex + ser = Series(range(len(index)), index=index, name="sth") buf = StringIO() - s.info(verbose=verbose, buf=buf) + ser.info(verbose=verbose, buf=buf) result = buf.getvalue() expected = textwrap.dedent( @@ -66,7 +62,7 @@ def test_info_series(verbose): expected += textwrap.dedent( f"""\ dtypes: int64(1) - memory usage: {s.memory_usage()}.0+ bytes + memory usage: {ser.memory_usage()}.0+ bytes """ ) assert result == expected diff --git a/pandas/tests/io/pytables/test_append.py b/pandas/tests/io/pytables/test_append.py index 6cfe8148ad034..e6dfe64df3864 100644 --- a/pandas/tests/io/pytables/test_append.py +++ b/pandas/tests/io/pytables/test_append.py @@ -12,7 +12,6 @@ import pandas as pd from pandas import ( DataFrame, - MultiIndex, Series, _testing as tm, concat, @@ -638,13 +637,9 @@ def check_col(key, name, size): tm.assert_frame_equal(result, expected) -def test_append_hierarchical(setup_path): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["foo", "bar"], - ) - df = DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) +def test_append_hierarchical(setup_path, multiindex_dataframe_random_data): + df = multiindex_dataframe_random_data + df.columns.name = None with ensure_clean_store(setup_path) as store: store.append("mi", df) diff --git a/pandas/tests/io/pytables/test_round_trip.py b/pandas/tests/io/pytables/test_round_trip.py index 9d7375b0f97e2..867525a7146b4 100644 --- a/pandas/tests/io/pytables/test_round_trip.py +++ b/pandas/tests/io/pytables/test_round_trip.py @@ -15,7 +15,6 @@ from pandas import ( DataFrame, Index, - MultiIndex, Series, _testing as tm, bdate_range, @@ -419,13 +418,8 @@ def test_can_serialize_dates(setup_path): _check_roundtrip(frame, tm.assert_frame_equal, path=setup_path) -def test_store_hierarchical(setup_path): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["foo", "bar"], - ) - frame = DataFrame(np.random.randn(10, 3), index=index, columns=["A", "B", "C"]) +def test_store_hierarchical(setup_path, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data _check_roundtrip(frame, tm.assert_frame_equal, path=setup_path) _check_roundtrip(frame.T, tm.assert_frame_equal, path=setup_path) diff --git a/pandas/tests/reshape/concat/test_index.py b/pandas/tests/reshape/concat/test_index.py index f8ad9d1084c53..a4d6a41c7eb50 100644 --- a/pandas/tests/reshape/concat/test_index.py +++ b/pandas/tests/reshape/concat/test_index.py @@ -190,17 +190,9 @@ def test_dups_index(self): class TestMultiIndexConcat: - def test_concat_multiindex_with_keys(self): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - frame = DataFrame( - np.random.randn(10, 3), - index=index, - columns=Index(["A", "B", "C"], name="exp"), - ) + def test_concat_multiindex_with_keys(self, multiindex_dataframe_random_data): + frame = multiindex_dataframe_random_data + index = frame.index result = concat([frame, frame], keys=[0, 1], names=["iteration"]) assert result.index.names == ("iteration",) + index.names diff --git a/pandas/tests/reshape/concat/test_series.py b/pandas/tests/reshape/concat/test_series.py index f53974d142bec..8fa5988720c6b 100644 --- a/pandas/tests/reshape/concat/test_series.py +++ b/pandas/tests/reshape/concat/test_series.py @@ -13,12 +13,6 @@ import pandas._testing as tm -@pytest.fixture(params=[True, False]) -def sort(request): - """Boolean sort keyword for concat and DataFrame.append.""" - return request.param - - class TestSeriesConcat: def test_concat_series(self): @@ -50,7 +44,7 @@ def test_concat_empty_and_non_empty_series_regression(self): result = concat([s1, s2]) tm.assert_series_equal(result, expected) - def test_concat_series_axis1(self, sort): + def test_concat_series_axis1(self): ts = tm.makeTimeSeries() pieces = [ts[:-2], ts[2:], ts[2:-2]] @@ -63,6 +57,7 @@ def test_concat_series_axis1(self, sort): expected = DataFrame(pieces, index=["A", "B", "C"]).T tm.assert_frame_equal(result, expected) + def test_concat_series_axis1_preserves_series_names(self): # preserve series names, #2489 s = Series(np.random.randn(5), name="A") s2 = Series(np.random.randn(5), name="B") @@ -75,6 +70,7 @@ def test_concat_series_axis1(self, sort): result = concat([s, s2], axis=1) tm.assert_index_equal(result.columns, Index(["A", 0], dtype="object")) + def test_concat_series_axis1_with_reindex(self, sort): # must reindex, #2603 s = Series(np.random.randn(3), index=["c", "a", "b"], name="A") s2 = Series(np.random.randn(4), index=["d", "a", "b", "c"], name="B") diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py index 6533cbb1f70cd..a091d072c0911 100644 --- a/pandas/tests/reshape/merge/test_join.py +++ b/pandas/tests/reshape/merge/test_join.py @@ -346,7 +346,7 @@ def test_join_multiindex(self): tm.assert_frame_equal(joined, expected) assert joined.index.names == index1.names - def test_join_inner_multiindex(self): + def test_join_inner_multiindex(self, lexsorted_two_level_string_multiindex): key1 = ["bar", "bar", "bar", "foo", "foo", "baz", "baz", "qux", "qux", "snap"] key2 = [ "two", @@ -364,11 +364,7 @@ def test_join_inner_multiindex(self): data = np.random.randn(len(key1)) data = DataFrame({"key1": key1, "key2": key2, "data": data}) - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) + index = lexsorted_two_level_string_multiindex to_join = DataFrame( np.random.randn(10, 3), index=index, columns=["j_one", "j_two", "j_three"] ) diff --git a/pandas/tests/reshape/merge/test_multi.py b/pandas/tests/reshape/merge/test_multi.py index d9143549e127d..b5945f7542077 100644 --- a/pandas/tests/reshape/merge/test_multi.py +++ b/pandas/tests/reshape/merge/test_multi.py @@ -26,17 +26,13 @@ def left(): @pytest.fixture -def right(): +def right(multiindex_dataframe_random_data): """right dataframe (multi-indexed) for multi-index join tests""" - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["key1", "key2"], - ) + df = multiindex_dataframe_random_data + df.index.names = ["key1", "key2"] - return DataFrame( - np.random.randn(10, 3), index=index, columns=["j_one", "j_two", "j_three"] - ) + df.columns = ["j_one", "j_two", "j_three"] + return df @pytest.fixture @@ -78,36 +74,6 @@ def idx_cols_multi(): class TestMergeMulti: - def setup_method(self): - self.index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - self.to_join = DataFrame( - np.random.randn(10, 3), - index=self.index, - columns=["j_one", "j_two", "j_three"], - ) - - # a little relevant example with NAs - key1 = ["bar", "bar", "bar", "foo", "foo", "baz", "baz", "qux", "qux", "snap"] - key2 = [ - "two", - "one", - "three", - "one", - "two", - "one", - "two", - "two", - "three", - "one", - ] - - data = np.random.randn(len(key1)) - self.data = DataFrame({"key1": key1, "key2": key2, "data": data}) - def test_merge_on_multikey(self, left, right, join_type): on_cols = ["key1", "key2"] result = left.join(right, on=on_cols, how=join_type).reset_index(drop=True) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 8a34882b1e5d4..8d0fba478bf66 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -8,7 +8,6 @@ from pandas import ( DataFrame, IndexSlice, - MultiIndex, Series, Timedelta, Timestamp, @@ -305,12 +304,8 @@ def test_setitem_mask_promote(): tm.assert_series_equal(ser, expected) -def test_multilevel_preserve_name(indexer_sl): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) +def test_multilevel_preserve_name(lexsorted_two_level_string_multiindex, indexer_sl): + index = lexsorted_two_level_string_multiindex ser = Series(np.random.randn(len(index)), index=index, name="sth") result = indexer_sl(ser)["foo"] diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index de34caa7b4387..930202a7ce1bd 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -11,7 +11,6 @@ Categorical, DataFrame, Index, - MultiIndex, Series, date_range, option_context, @@ -22,13 +21,9 @@ class TestSeriesRepr: - def test_multilevel_name_print(self): - index = MultiIndex( - levels=[["foo", "bar", "baz", "qux"], ["one", "two", "three"]], - codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], - names=["first", "second"], - ) - s = Series(range(len(index)), index=index, name="sth") + def test_multilevel_name_print(self, lexsorted_two_level_string_multiindex): + index = lexsorted_two_level_string_multiindex + ser = Series(range(len(index)), index=index, name="sth") expected = [ "first second", "foo one 0", @@ -44,7 +39,7 @@ def test_multilevel_name_print(self): "Name: sth, dtype: int64", ] expected = "\n".join(expected) - assert repr(s) == expected + assert repr(ser) == expected def test_name_printing(self): # Test small Series.