From 88e794be362b9c60dfd432644345284ebfdae5ca Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Tue, 24 Mar 2020 16:56:01 +0100 Subject: [PATCH] move _get_cython_table_params into pandas/_testing.py --- pandas/_testing.py | 31 +++++++++++++++++++++++++++++ pandas/conftest.py | 33 +------------------------------ pandas/tests/frame/test_apply.py | 11 +++++------ pandas/tests/series/test_apply.py | 15 +++++++------- 4 files changed, 44 insertions(+), 46 deletions(-) diff --git a/pandas/_testing.py b/pandas/_testing.py index e69263b81e1aa..1f6b645c821c8 100644 --- a/pandas/_testing.py +++ b/pandas/_testing.py @@ -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 diff --git a/pandas/conftest.py b/pandas/conftest.py index 903e1a5dec132..82f1a9c242bb2 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -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 @@ -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) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 6dee4424f1cec..e328523253144 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -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 @@ -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")), @@ -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])), @@ -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]])), @@ -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]] ), ) diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 3c2cb5275f3a8..63acc12877a63 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -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 @@ -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), @@ -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), @@ -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"), @@ -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"]))] ), ), @@ -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