Skip to content

Commit aabf219

Browse files
mroeschkephofl
authored andcommitted
TST: Clean pytest fixtures (#56088)
* TST: Clean/moved unused/sparsely use fixtures * Refactor xml_data_path * More fixture fixes * inline tsframe * Simplify ext fixtures
1 parent 188b4af commit aabf219

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+635
-690
lines changed

pandas/conftest.py

-105
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from decimal import Decimal
3131
import operator
3232
import os
33-
from pathlib import Path
3433
from typing import (
3534
TYPE_CHECKING,
3635
Callable,
@@ -775,23 +774,6 @@ def series_with_simple_index(index) -> Series:
775774
return _create_series(index)
776775

777776

778-
@pytest.fixture
779-
def series_with_multilevel_index() -> Series:
780-
"""
781-
Fixture with a Series with a 2-level MultiIndex.
782-
"""
783-
arrays = [
784-
["bar", "bar", "baz", "baz", "qux", "qux", "foo", "foo"],
785-
["one", "two", "one", "two", "one", "two", "one", "two"],
786-
]
787-
tuples = zip(*arrays)
788-
index = MultiIndex.from_tuples(tuples)
789-
data = np.random.default_rng(2).standard_normal(8)
790-
ser = Series(data, index=index)
791-
ser.iloc[3] = np.nan
792-
return ser
793-
794-
795777
_narrow_series = {
796778
f"{dtype.__name__}-series": tm.make_rand_series(name="a", dtype=dtype)
797779
for dtype in tm.NARROW_NP_DTYPES
@@ -865,35 +847,6 @@ def int_frame() -> DataFrame:
865847
return DataFrame(tm.getSeriesData()).astype("int64")
866848

867849

868-
@pytest.fixture
869-
def datetime_frame() -> DataFrame:
870-
"""
871-
Fixture for DataFrame of floats with DatetimeIndex
872-
873-
Columns are ['A', 'B', 'C', 'D']
874-
875-
A B C D
876-
2000-01-03 -1.122153 0.468535 0.122226 1.693711
877-
2000-01-04 0.189378 0.486100 0.007864 -1.216052
878-
2000-01-05 0.041401 -0.835752 -0.035279 -0.414357
879-
2000-01-06 0.430050 0.894352 0.090719 0.036939
880-
2000-01-07 -0.620982 -0.668211 -0.706153 1.466335
881-
2000-01-10 -0.752633 0.328434 -0.815325 0.699674
882-
2000-01-11 -2.236969 0.615737 -0.829076 -1.196106
883-
... ... ... ... ...
884-
2000-02-03 1.642618 -0.579288 0.046005 1.385249
885-
2000-02-04 -0.544873 -1.160962 -0.284071 -1.418351
886-
2000-02-07 -2.656149 -0.601387 1.410148 0.444150
887-
2000-02-08 -1.201881 -1.289040 0.772992 -1.445300
888-
2000-02-09 1.377373 0.398619 1.008453 -0.928207
889-
2000-02-10 0.473194 -0.636677 0.984058 0.511519
890-
2000-02-11 -0.965556 0.408313 -1.312844 -0.381948
891-
892-
[30 rows x 4 columns]
893-
"""
894-
return DataFrame(tm.getTimeSeriesData())
895-
896-
897850
@pytest.fixture
898851
def float_frame() -> DataFrame:
899852
"""
@@ -923,24 +876,6 @@ def float_frame() -> DataFrame:
923876
return DataFrame(tm.getSeriesData())
924877

925878

926-
@pytest.fixture
927-
def mixed_type_frame() -> DataFrame:
928-
"""
929-
Fixture for DataFrame of float/int/string columns with RangeIndex
930-
Columns are ['a', 'b', 'c', 'float32', 'int32'].
931-
"""
932-
return DataFrame(
933-
{
934-
"a": 1.0,
935-
"b": 2,
936-
"c": "foo",
937-
"float32": np.array([1.0] * 10, dtype="float32"),
938-
"int32": np.array([1] * 10, dtype="int32"),
939-
},
940-
index=np.arange(10),
941-
)
942-
943-
944879
@pytest.fixture
945880
def rand_series_with_duplicate_datetimeindex() -> Series:
946881
"""
@@ -1174,16 +1109,6 @@ def strict_data_files(pytestconfig):
11741109
return pytestconfig.getoption("--no-strict-data-files")
11751110

11761111

1177-
@pytest.fixture
1178-
def tests_path() -> Path:
1179-
return Path(__file__).parent / "tests"
1180-
1181-
1182-
@pytest.fixture
1183-
def tests_io_data_path(tests_path) -> Path:
1184-
return tests_path / "io" / "data"
1185-
1186-
11871112
@pytest.fixture
11881113
def datapath(strict_data_files: str) -> Callable[..., str]:
11891114
"""
@@ -1218,14 +1143,6 @@ def deco(*args):
12181143
return deco
12191144

12201145

1221-
@pytest.fixture
1222-
def iris(datapath) -> DataFrame:
1223-
"""
1224-
The iris dataset as a DataFrame.
1225-
"""
1226-
return pd.read_csv(datapath("io", "data", "csv", "iris.csv"))
1227-
1228-
12291146
# ----------------------------------------------------------------
12301147
# Time zones
12311148
# ----------------------------------------------------------------
@@ -1905,28 +1822,6 @@ def sort_by_key(request):
19051822
return request.param
19061823

19071824

1908-
@pytest.fixture()
1909-
def fsspectest():
1910-
pytest.importorskip("fsspec")
1911-
from fsspec import register_implementation
1912-
from fsspec.implementations.memory import MemoryFileSystem
1913-
from fsspec.registry import _registry as registry
1914-
1915-
class TestMemoryFS(MemoryFileSystem):
1916-
protocol = "testmem"
1917-
test = [None]
1918-
1919-
def __init__(self, **kwargs) -> None:
1920-
self.test[0] = kwargs.pop("test", None)
1921-
super().__init__(**kwargs)
1922-
1923-
register_implementation("testmem", TestMemoryFS, clobber=True)
1924-
yield TestMemoryFS()
1925-
registry.pop("testmem", None)
1926-
TestMemoryFS.test[0] = None
1927-
TestMemoryFS.store.clear()
1928-
1929-
19301825
@pytest.fixture(
19311826
params=[
19321827
("foo", None, None),

pandas/tests/apply/conftest.py

-30
This file was deleted.

pandas/tests/apply/test_frame_apply.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,27 @@
1818
from pandas.tests.frame.common import zip_frames
1919

2020

21+
@pytest.fixture
22+
def int_frame_const_col():
23+
"""
24+
Fixture for DataFrame of ints which are constant per column
25+
26+
Columns are ['A', 'B', 'C'], with values (per column): [1, 2, 3]
27+
"""
28+
df = DataFrame(
29+
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
30+
columns=["A", "B", "C"],
31+
)
32+
return df
33+
34+
35+
@pytest.fixture(params=["python", pytest.param("numba", marks=pytest.mark.single_cpu)])
36+
def engine(request):
37+
if request.param == "numba":
38+
pytest.importorskip("numba")
39+
return request.param
40+
41+
2142
def test_apply(float_frame, engine, request):
2243
if engine == "numba":
2344
mark = pytest.mark.xfail(reason="numba engine not supporting numpy ufunc yet")
@@ -269,7 +290,7 @@ def test_apply_raw_float_frame_no_reduction(float_frame, engine):
269290

270291

271292
@pytest.mark.parametrize("axis", [0, 1])
272-
def test_apply_raw_mixed_type_frame(mixed_type_frame, axis, engine):
293+
def test_apply_raw_mixed_type_frame(axis, engine):
273294
if engine == "numba":
274295
pytest.skip("isinstance check doesn't work with numba")
275296

@@ -278,7 +299,17 @@ def _assert_raw(x):
278299
assert x.ndim == 1
279300

280301
# Mixed dtype (GH-32423)
281-
mixed_type_frame.apply(_assert_raw, axis=axis, engine=engine, raw=True)
302+
df = DataFrame(
303+
{
304+
"a": 1.0,
305+
"b": 2,
306+
"c": "foo",
307+
"float32": np.array([1.0] * 10, dtype="float32"),
308+
"int32": np.array([1] * 10, dtype="int32"),
309+
},
310+
index=np.arange(10),
311+
)
312+
df.apply(_assert_raw, axis=axis, engine=engine, raw=True)
282313

283314

284315
def test_apply_axis1(float_frame):

pandas/tests/apply/test_invalid_arg.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525

2626
@pytest.mark.parametrize("result_type", ["foo", 1])
27-
def test_result_type_error(result_type, int_frame_const_col):
27+
def test_result_type_error(result_type):
2828
# allowed result_type
29-
df = int_frame_const_col
29+
df = DataFrame(
30+
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
31+
columns=["A", "B", "C"],
32+
)
3033

3134
msg = (
3235
"invalid value for result_type, must be one of "
@@ -282,8 +285,11 @@ def test_transform_none_to_type():
282285
lambda x: Series([1, 2]),
283286
],
284287
)
285-
def test_apply_broadcast_error(int_frame_const_col, func):
286-
df = int_frame_const_col
288+
def test_apply_broadcast_error(func):
289+
df = DataFrame(
290+
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
291+
columns=["A", "B", "C"],
292+
)
287293

288294
# > 1 ndim
289295
msg = "too many dims to broadcast|cannot broadcast result"

pandas/tests/apply/test_numba.py

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
pytestmark = [td.skip_if_no("numba"), pytest.mark.single_cpu]
1313

1414

15+
@pytest.fixture(params=[0, 1])
16+
def apply_axis(request):
17+
return request.param
18+
19+
1520
def test_numba_vs_python_noop(float_frame, apply_axis):
1621
func = lambda x: x
1722
result = float_frame.apply(func, engine="numba", axis=apply_axis)

pandas/tests/arithmetic/conftest.py

+1-72
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
import pytest
33

44
import pandas as pd
5-
from pandas import (
6-
Index,
7-
RangeIndex,
8-
)
9-
import pandas._testing as tm
5+
from pandas import Index
106

117

128
@pytest.fixture(params=[1, np.array(1, dtype=np.int64)])
@@ -63,27 +59,6 @@ def zero(request):
6359
return request.param
6460

6561

66-
# ------------------------------------------------------------------
67-
# Vector Fixtures
68-
69-
70-
@pytest.fixture(
71-
params=[
72-
# TODO: add more dtypes here
73-
Index(np.arange(5, dtype="float64")),
74-
Index(np.arange(5, dtype="int64")),
75-
Index(np.arange(5, dtype="uint64")),
76-
RangeIndex(5),
77-
],
78-
ids=lambda x: type(x).__name__,
79-
)
80-
def numeric_idx(request):
81-
"""
82-
Several types of numeric-dtypes Index objects
83-
"""
84-
return request.param
85-
86-
8762
# ------------------------------------------------------------------
8863
# Scalar Fixtures
8964

@@ -148,22 +123,6 @@ def two_hours(request):
148123
]
149124

150125

151-
@pytest.fixture(
152-
params=[
153-
pd.Timedelta(minutes=30).to_pytimedelta(),
154-
np.timedelta64(30, "s"),
155-
pd.Timedelta(seconds=30),
156-
]
157-
+ _common_mismatch
158-
)
159-
def not_hourly(request):
160-
"""
161-
Several timedelta-like and DateOffset instances that are _not_
162-
compatible with Hourly frequencies.
163-
"""
164-
return request.param
165-
166-
167126
@pytest.fixture(
168127
params=[
169128
np.timedelta64(4, "h"),
@@ -178,33 +137,3 @@ def not_daily(request):
178137
compatible with Daily frequencies.
179138
"""
180139
return request.param
181-
182-
183-
@pytest.fixture(
184-
params=[
185-
np.timedelta64(365, "D"),
186-
pd.Timedelta(days=365).to_pytimedelta(),
187-
pd.Timedelta(days=365),
188-
]
189-
+ _common_mismatch
190-
)
191-
def mismatched_freq(request):
192-
"""
193-
Several timedelta-like and DateOffset instances that are _not_
194-
compatible with Monthly or Annual frequencies.
195-
"""
196-
return request.param
197-
198-
199-
# ------------------------------------------------------------------
200-
201-
202-
@pytest.fixture(
203-
params=[Index, pd.Series, tm.to_array, np.array, list], ids=lambda x: x.__name__
204-
)
205-
def box_1d_array(request):
206-
"""
207-
Fixture to test behavior for Index, Series, tm.to_array, numpy Array and list
208-
classes
209-
"""
210-
return request.param

0 commit comments

Comments
 (0)