Skip to content

TST: Move _get_cython_table_params into pandas/_testing.py #32981

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
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
31 changes: 31 additions & 0 deletions pandas/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2662,3 +2662,34 @@ def external_error_raised(
import pytest

return pytest.raises(expected_exception, match=None)


cython_table = pd.core.base.SelectionMixin._cython_table.items()


def get_cython_table_params(ndframe, func_names_and_expected):
"""
Combine frame, functions from SelectionMixin._cython_table
keys and expected result.

Parameters
----------
ndframe : DataFrame or Series
func_names_and_expected : Sequence of two items
The first item is a name of a NDFrame method ('sum', 'prod') etc.
The second item is the expected return value.

Returns
-------
list
List of three items (DataFrame, function, expected result)
"""
results = []
for func_name, expected in func_names_and_expected:
results.append((ndframe, func_name, expected))
results += [
(ndframe, func, expected)
for func, name in cython_table
if name == func_name
]
return results
33 changes: 1 addition & 32 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,7 @@ def spmatrix(request):
return getattr(sparse, request.param + "_matrix")


_cython_table = pd.core.base.SelectionMixin._cython_table.items()


@pytest.fixture(params=list(_cython_table))
@pytest.fixture(params=list(tm.cython_table))
def cython_table_items(request):
"""
Yields a tuple of a function and its corresponding name. Correspond to
Expand All @@ -1131,34 +1128,6 @@ def cython_table_items(request):
return request.param


def _get_cython_table_params(ndframe, func_names_and_expected):
"""
Combine frame, functions from SelectionMixin._cython_table
keys and expected result.

Parameters
----------
ndframe : DataFrame or Series
func_names_and_expected : Sequence of two items
The first item is a name of a NDFrame method ('sum', 'prod') etc.
The second item is the expected return value.

Returns
-------
list
List of three items (DataFrame, function, expected result)
"""
results = []
for func_name, expected in func_names_and_expected:
results.append((ndframe, func_name, expected))
results += [
(ndframe, func, expected)
for func, name in _cython_table
if name == func_name
]
return results


@pytest.fixture(
params=[
getattr(pd.offsets, o)
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/frame/test_apply.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, Timestamp, date_range, notna
import pandas._testing as tm
from pandas.conftest import _get_cython_table_params
from pandas.core.apply import frame_apply
from pandas.core.base import SpecificationError

Expand Down Expand Up @@ -1323,7 +1322,7 @@ def func(group_col):
@pytest.mark.parametrize(
"df, func, expected",
chain(
_get_cython_table_params(
tm.get_cython_table_params(
DataFrame(),
[
("sum", Series(dtype="float64")),
Expand All @@ -1338,7 +1337,7 @@ def func(group_col):
("median", Series(dtype="float64")),
],
),
_get_cython_table_params(
tm.get_cython_table_params(
DataFrame([[np.nan, 1], [1, 2]]),
[
("sum", Series([1.0, 3])),
Expand All @@ -1365,10 +1364,10 @@ def test_agg_cython_table(self, df, func, expected, axis):
@pytest.mark.parametrize(
"df, func, expected",
chain(
_get_cython_table_params(
tm.get_cython_table_params(
DataFrame(), [("cumprod", DataFrame()), ("cumsum", DataFrame())]
),
_get_cython_table_params(
tm.get_cython_table_params(
DataFrame([[np.nan, 1], [1, 2]]),
[
("cumprod", DataFrame([[np.nan, 1], [1, 2]])),
Expand All @@ -1390,7 +1389,7 @@ def test_agg_cython_table_transform(self, df, func, expected, axis):

@pytest.mark.parametrize(
"df, func, expected",
_get_cython_table_params(
tm.get_cython_table_params(
DataFrame([["a", "b"], ["b", "a"]]), [["cumprod", TypeError]]
),
)
Expand Down
15 changes: 7 additions & 8 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import pandas as pd
from pandas import DataFrame, Index, Series, isna
import pandas._testing as tm
from pandas.conftest import _get_cython_table_params
from pandas.core.base import SpecificationError


Expand Down Expand Up @@ -356,7 +355,7 @@ def test_non_callable_aggregates(self):
@pytest.mark.parametrize(
"series, func, expected",
chain(
_get_cython_table_params(
tm.get_cython_table_params(
Series(dtype=np.float64),
[
("sum", 0),
Expand All @@ -371,7 +370,7 @@ def test_non_callable_aggregates(self):
("median", np.nan),
],
),
_get_cython_table_params(
tm.get_cython_table_params(
Series([np.nan, 1, 2, 3]),
[
("sum", 6),
Expand All @@ -386,7 +385,7 @@ def test_non_callable_aggregates(self):
("median", 2),
],
),
_get_cython_table_params(
tm.get_cython_table_params(
Series("a b c".split()),
[
("sum", "abc"),
Expand All @@ -411,21 +410,21 @@ def test_agg_cython_table(self, series, func, expected):
@pytest.mark.parametrize(
"series, func, expected",
chain(
_get_cython_table_params(
tm.get_cython_table_params(
Series(dtype=np.float64),
[
("cumprod", Series([], Index([]), dtype=np.float64)),
("cumsum", Series([], Index([]), dtype=np.float64)),
],
),
_get_cython_table_params(
tm.get_cython_table_params(
Series([np.nan, 1, 2, 3]),
[
("cumprod", Series([np.nan, 1, 2, 6])),
("cumsum", Series([np.nan, 1, 3, 6])),
],
),
_get_cython_table_params(
tm.get_cython_table_params(
Series("a b c".split()), [("cumsum", Series(["a", "ab", "abc"]))]
),
),
Expand All @@ -440,7 +439,7 @@ def test_agg_cython_table_transform(self, series, func, expected):
@pytest.mark.parametrize(
"series, func, expected",
chain(
_get_cython_table_params(
tm.get_cython_table_params(
Series("a b c".split()),
[
("mean", TypeError), # mean raises TypeError
Expand Down