Skip to content
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

TST: Clean pytest fixtures #56088

Merged
merged 8 commits into from
Nov 21, 2023
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
105 changes: 0 additions & 105 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from decimal import Decimal
import operator
import os
from pathlib import Path
from typing import (
TYPE_CHECKING,
Callable,
Expand Down Expand Up @@ -775,23 +774,6 @@ def series_with_simple_index(index) -> Series:
return _create_series(index)


@pytest.fixture
def series_with_multilevel_index() -> Series:
"""
Fixture with a Series with a 2-level MultiIndex.
"""
arrays = [
["bar", "bar", "baz", "baz", "qux", "qux", "foo", "foo"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
tuples = zip(*arrays)
index = MultiIndex.from_tuples(tuples)
data = np.random.default_rng(2).standard_normal(8)
ser = Series(data, index=index)
ser.iloc[3] = np.nan
return ser


_narrow_series = {
f"{dtype.__name__}-series": tm.make_rand_series(name="a", dtype=dtype)
for dtype in tm.NARROW_NP_DTYPES
Expand Down Expand Up @@ -865,35 +847,6 @@ def int_frame() -> DataFrame:
return DataFrame(tm.getSeriesData()).astype("int64")


@pytest.fixture
def datetime_frame() -> DataFrame:
"""
Fixture for DataFrame of floats with DatetimeIndex

Columns are ['A', 'B', 'C', 'D']

A B C D
2000-01-03 -1.122153 0.468535 0.122226 1.693711
2000-01-04 0.189378 0.486100 0.007864 -1.216052
2000-01-05 0.041401 -0.835752 -0.035279 -0.414357
2000-01-06 0.430050 0.894352 0.090719 0.036939
2000-01-07 -0.620982 -0.668211 -0.706153 1.466335
2000-01-10 -0.752633 0.328434 -0.815325 0.699674
2000-01-11 -2.236969 0.615737 -0.829076 -1.196106
... ... ... ... ...
2000-02-03 1.642618 -0.579288 0.046005 1.385249
2000-02-04 -0.544873 -1.160962 -0.284071 -1.418351
2000-02-07 -2.656149 -0.601387 1.410148 0.444150
2000-02-08 -1.201881 -1.289040 0.772992 -1.445300
2000-02-09 1.377373 0.398619 1.008453 -0.928207
2000-02-10 0.473194 -0.636677 0.984058 0.511519
2000-02-11 -0.965556 0.408313 -1.312844 -0.381948

[30 rows x 4 columns]
"""
return DataFrame(tm.getTimeSeriesData())


@pytest.fixture
def float_frame() -> DataFrame:
"""
Expand Down Expand Up @@ -923,24 +876,6 @@ def float_frame() -> DataFrame:
return DataFrame(tm.getSeriesData())


@pytest.fixture
def mixed_type_frame() -> DataFrame:
"""
Fixture for DataFrame of float/int/string columns with RangeIndex
Columns are ['a', 'b', 'c', 'float32', 'int32'].
"""
return DataFrame(
{
"a": 1.0,
"b": 2,
"c": "foo",
"float32": np.array([1.0] * 10, dtype="float32"),
"int32": np.array([1] * 10, dtype="int32"),
},
index=np.arange(10),
)


@pytest.fixture
def rand_series_with_duplicate_datetimeindex() -> Series:
"""
Expand Down Expand Up @@ -1174,16 +1109,6 @@ def strict_data_files(pytestconfig):
return pytestconfig.getoption("--no-strict-data-files")


@pytest.fixture
def tests_path() -> Path:
return Path(__file__).parent / "tests"


@pytest.fixture
def tests_io_data_path(tests_path) -> Path:
return tests_path / "io" / "data"


@pytest.fixture
def datapath(strict_data_files: str) -> Callable[..., str]:
"""
Expand Down Expand Up @@ -1218,14 +1143,6 @@ def deco(*args):
return deco


@pytest.fixture
def iris(datapath) -> DataFrame:
"""
The iris dataset as a DataFrame.
"""
return pd.read_csv(datapath("io", "data", "csv", "iris.csv"))


# ----------------------------------------------------------------
# Time zones
# ----------------------------------------------------------------
Expand Down Expand Up @@ -1905,28 +1822,6 @@ def sort_by_key(request):
return request.param


@pytest.fixture()
def fsspectest():
pytest.importorskip("fsspec")
from fsspec import register_implementation
from fsspec.implementations.memory import MemoryFileSystem
from fsspec.registry import _registry as registry

class TestMemoryFS(MemoryFileSystem):
protocol = "testmem"
test = [None]

def __init__(self, **kwargs) -> None:
self.test[0] = kwargs.pop("test", None)
super().__init__(**kwargs)

register_implementation("testmem", TestMemoryFS, clobber=True)
yield TestMemoryFS()
registry.pop("testmem", None)
TestMemoryFS.test[0] = None
TestMemoryFS.store.clear()


@pytest.fixture(
params=[
("foo", None, None),
Expand Down
30 changes: 0 additions & 30 deletions pandas/tests/apply/conftest.py

This file was deleted.

35 changes: 33 additions & 2 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@
from pandas.tests.frame.common import zip_frames


@pytest.fixture
def int_frame_const_col():
"""
Fixture for DataFrame of ints which are constant per column

Columns are ['A', 'B', 'C'], with values (per column): [1, 2, 3]
"""
df = DataFrame(
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
columns=["A", "B", "C"],
)
return df


@pytest.fixture(params=["python", pytest.param("numba", marks=pytest.mark.single_cpu)])
def engine(request):
if request.param == "numba":
pytest.importorskip("numba")
return request.param


def test_apply(float_frame, engine, request):
if engine == "numba":
mark = pytest.mark.xfail(reason="numba engine not supporting numpy ufunc yet")
Expand Down Expand Up @@ -269,7 +290,7 @@ def test_apply_raw_float_frame_no_reduction(float_frame, engine):


@pytest.mark.parametrize("axis", [0, 1])
def test_apply_raw_mixed_type_frame(mixed_type_frame, axis, engine):
def test_apply_raw_mixed_type_frame(axis, engine):
if engine == "numba":
pytest.skip("isinstance check doesn't work with numba")

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

# Mixed dtype (GH-32423)
mixed_type_frame.apply(_assert_raw, axis=axis, engine=engine, raw=True)
df = DataFrame(
{
"a": 1.0,
"b": 2,
"c": "foo",
"float32": np.array([1.0] * 10, dtype="float32"),
"int32": np.array([1] * 10, dtype="int32"),
},
index=np.arange(10),
)
df.apply(_assert_raw, axis=axis, engine=engine, raw=True)


def test_apply_axis1(float_frame):
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/apply/test_invalid_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@


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

msg = (
"invalid value for result_type, must be one of "
Expand Down Expand Up @@ -282,8 +285,11 @@ def test_transform_none_to_type():
lambda x: Series([1, 2]),
],
)
def test_apply_broadcast_error(int_frame_const_col, func):
df = int_frame_const_col
def test_apply_broadcast_error(func):
df = DataFrame(
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
columns=["A", "B", "C"],
)

# > 1 ndim
msg = "too many dims to broadcast|cannot broadcast result"
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/apply/test_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
pytestmark = [td.skip_if_no("numba"), pytest.mark.single_cpu]


@pytest.fixture(params=[0, 1])
def apply_axis(request):
return request.param


def test_numba_vs_python_noop(float_frame, apply_axis):
func = lambda x: x
result = float_frame.apply(func, engine="numba", axis=apply_axis)
Expand Down
73 changes: 1 addition & 72 deletions pandas/tests/arithmetic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import pytest

import pandas as pd
from pandas import (
Index,
RangeIndex,
)
import pandas._testing as tm
from pandas import Index


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


# ------------------------------------------------------------------
# Vector Fixtures


@pytest.fixture(
params=[
# TODO: add more dtypes here
Index(np.arange(5, dtype="float64")),
Index(np.arange(5, dtype="int64")),
Index(np.arange(5, dtype="uint64")),
RangeIndex(5),
],
ids=lambda x: type(x).__name__,
)
def numeric_idx(request):
"""
Several types of numeric-dtypes Index objects
"""
return request.param


# ------------------------------------------------------------------
# Scalar Fixtures

Expand Down Expand Up @@ -148,22 +123,6 @@ def two_hours(request):
]


@pytest.fixture(
params=[
pd.Timedelta(minutes=30).to_pytimedelta(),
np.timedelta64(30, "s"),
pd.Timedelta(seconds=30),
]
+ _common_mismatch
)
def not_hourly(request):
"""
Several timedelta-like and DateOffset instances that are _not_
compatible with Hourly frequencies.
"""
return request.param


@pytest.fixture(
params=[
np.timedelta64(4, "h"),
Expand All @@ -178,33 +137,3 @@ def not_daily(request):
compatible with Daily frequencies.
"""
return request.param


@pytest.fixture(
params=[
np.timedelta64(365, "D"),
pd.Timedelta(days=365).to_pytimedelta(),
pd.Timedelta(days=365),
]
+ _common_mismatch
)
def mismatched_freq(request):
"""
Several timedelta-like and DateOffset instances that are _not_
compatible with Monthly or Annual frequencies.
"""
return request.param


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


@pytest.fixture(
params=[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
classes
"""
return request.param
Loading
Loading