diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index 0036c3ab25bf1..69b2b0876fc80 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -3,7 +3,6 @@ from decimal import Decimal import operator import os -import re from sys import byteorder from typing import ( TYPE_CHECKING, @@ -27,7 +26,6 @@ import pandas as pd from pandas import ( ArrowDtype, - Categorical, DataFrame, Index, MultiIndex, @@ -243,9 +241,6 @@ ALL_PYARROW_DTYPES = [] -EMPTY_STRING_PATTERN = re.compile("^$") - - arithmetic_dunder_methods = [ "__add__", "__radd__", @@ -353,42 +348,6 @@ def _constructor_sliced(self): return lambda *args, **kwargs: SubclassedSeries(*args, **kwargs) -class SubclassedCategorical(Categorical): - pass - - -def _make_skipna_wrapper(alternative, skipna_alternative=None): - """ - Create a function for calling on an array. - - Parameters - ---------- - alternative : function - The function to be called on the array with no NaNs. - Only used when 'skipna_alternative' is None. - skipna_alternative : function - The function to be called on the original array - - Returns - ------- - function - """ - if skipna_alternative: - - def skipna_wrapper(x): - return skipna_alternative(x.values) - - else: - - def skipna_wrapper(x): - nona = x.dropna() - if len(nona) == 0: - return np.nan - return alternative(nona) - - return skipna_wrapper - - def convert_rows_list_to_csv_str(rows_list: list[str]) -> str: """ Convert list of CSV rows to single CSV-formatted string for current OS. @@ -621,7 +580,6 @@ def shares_memory(left, right) -> bool: "convert_rows_list_to_csv_str", "DATETIME64_DTYPES", "decompress_file", - "EMPTY_STRING_PATTERN", "ENDIAN", "ensure_clean", "external_error_raised", @@ -654,7 +612,6 @@ def shares_memory(left, right) -> bool: "SIGNED_INT_EA_DTYPES", "SIGNED_INT_NUMPY_DTYPES", "STRING_DTYPES", - "SubclassedCategorical", "SubclassedDataFrame", "SubclassedSeries", "TIMEDELTA64_DTYPES", diff --git a/pandas/tests/arrays/categorical/test_subclass.py b/pandas/tests/arrays/categorical/test_subclass.py index 48325395faad8..5b0c0a44e655d 100644 --- a/pandas/tests/arrays/categorical/test_subclass.py +++ b/pandas/tests/arrays/categorical/test_subclass.py @@ -2,21 +2,25 @@ import pandas._testing as tm +class SubclassedCategorical(Categorical): + pass + + class TestCategoricalSubclassing: def test_constructor(self): - sc = tm.SubclassedCategorical(["a", "b", "c"]) - assert isinstance(sc, tm.SubclassedCategorical) + sc = SubclassedCategorical(["a", "b", "c"]) + assert isinstance(sc, SubclassedCategorical) tm.assert_categorical_equal(sc, Categorical(["a", "b", "c"])) def test_from_codes(self): - sc = tm.SubclassedCategorical.from_codes([1, 0, 2], ["a", "b", "c"]) - assert isinstance(sc, tm.SubclassedCategorical) + sc = SubclassedCategorical.from_codes([1, 0, 2], ["a", "b", "c"]) + assert isinstance(sc, SubclassedCategorical) exp = Categorical.from_codes([1, 0, 2], ["a", "b", "c"]) tm.assert_categorical_equal(sc, exp) def test_map(self): - sc = tm.SubclassedCategorical(["a", "b", "c"]) + sc = SubclassedCategorical(["a", "b", "c"]) res = sc.map(lambda x: x.upper(), na_action=None) - assert isinstance(res, tm.SubclassedCategorical) + assert isinstance(res, SubclassedCategorical) exp = Categorical(["A", "B", "C"]) tm.assert_categorical_equal(res, exp) diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py index b079c331eeebb..3b1a751a738f9 100644 --- a/pandas/tests/frame/test_reductions.py +++ b/pandas/tests/frame/test_reductions.py @@ -40,6 +40,38 @@ is_windows_or_is32 = is_platform_windows() or not IS64 +def make_skipna_wrapper(alternative, skipna_alternative=None): + """ + Create a function for calling on an array. + + Parameters + ---------- + alternative : function + The function to be called on the array with no NaNs. + Only used when 'skipna_alternative' is None. + skipna_alternative : function + The function to be called on the original array + + Returns + ------- + function + """ + if skipna_alternative: + + def skipna_wrapper(x): + return skipna_alternative(x.values) + + else: + + def skipna_wrapper(x): + nona = x.dropna() + if len(nona) == 0: + return np.nan + return alternative(nona) + + return skipna_wrapper + + def assert_stat_op_calc( opname, alternative, @@ -96,7 +128,7 @@ def assert_stat_op_calc( def wrapper(x): return alternative(x.values) - skipna_wrapper = tm._make_skipna_wrapper(alternative, skipna_alternative) + skipna_wrapper = make_skipna_wrapper(alternative, skipna_alternative) result0 = f(axis=0, skipna=False) result1 = f(axis=1, skipna=False) tm.assert_series_equal( diff --git a/pandas/tests/io/json/test_normalize.py b/pandas/tests/io/json/test_normalize.py index 316f262885424..3c6517a872b76 100644 --- a/pandas/tests/io/json/test_normalize.py +++ b/pandas/tests/io/json/test_normalize.py @@ -200,7 +200,7 @@ def test_empty_array(self): ) def test_accepted_input(self, data, record_path, exception_type): if exception_type is not None: - with pytest.raises(exception_type, match=tm.EMPTY_STRING_PATTERN): + with pytest.raises(exception_type, match=""): json_normalize(data, record_path=record_path) else: result = json_normalize(data, record_path=record_path) diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py index a75e77a5e2714..9d13ebf740eab 100644 --- a/pandas/tests/series/test_ufunc.py +++ b/pandas/tests/series/test_ufunc.py @@ -413,7 +413,7 @@ def test_outer(): ser = pd.Series([1, 2, 3]) obj = np.array([1, 2, 3]) - with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN): + with pytest.raises(NotImplementedError, match=""): np.subtract.outer(ser, obj)