Skip to content

TST: de-duplicate fixtures #44699

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 1, 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
14 changes: 11 additions & 3 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
20 changes: 6 additions & 14 deletions pandas/tests/arithmetic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


# ------------------------------------------------------------------


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 2 additions & 11 deletions pandas/tests/frame/methods/test_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
40 changes: 6 additions & 34 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
CategoricalDtype,
CategoricalIndex,
DataFrame,
Index,
IntervalIndex,
MultiIndex,
RangeIndex,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down
14 changes: 3 additions & 11 deletions pandas/tests/groupby/conftest.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
29 changes: 5 additions & 24 deletions pandas/tests/groupby/test_allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
date_range,
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 9 additions & 11 deletions pandas/tests/indexes/multi/test_monotonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]],
Expand Down
13 changes: 5 additions & 8 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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)

Expand Down
14 changes: 5 additions & 9 deletions pandas/tests/io/formats/test_series_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions pandas/tests/io/pytables/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import pandas as pd
from pandas import (
DataFrame,
MultiIndex,
Series,
_testing as tm,
concat,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions pandas/tests/io/pytables/test_round_trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
_testing as tm,
bdate_range,
Expand Down Expand Up @@ -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)
Expand Down
Loading