Skip to content

TST/CLN: Remove seldomly use items in _testing/__init__.py #56344

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
merged 3 commits into from
Dec 6, 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
43 changes: 0 additions & 43 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from decimal import Decimal
import operator
import os
import re
from sys import byteorder
from typing import (
TYPE_CHECKING,
Expand All @@ -27,7 +26,6 @@
import pandas as pd
from pandas import (
ArrowDtype,
Categorical,
DataFrame,
Index,
MultiIndex,
Expand Down Expand Up @@ -243,9 +241,6 @@
ALL_PYARROW_DTYPES = []


EMPTY_STRING_PATTERN = re.compile("^$")


arithmetic_dunder_methods = [
"__add__",
"__radd__",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
16 changes: 10 additions & 6 deletions pandas/tests/arrays/categorical/test_subclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
34 changes: 33 additions & 1 deletion pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down