Skip to content

TST/REF: Consolidate tests in apply.test_invalid_arg #40092

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 4 commits into from
Mar 3, 2021
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
18 changes: 18 additions & 0 deletions pandas/tests/apply/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
import pytest

from pandas import DataFrame


@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
163 changes: 0 additions & 163 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@
Series,
Timestamp,
date_range,
notna,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError
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


def test_apply(float_frame):
with np.errstate(all="ignore"):
# ufunc
Expand Down Expand Up @@ -192,17 +176,6 @@ def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"how, args", [("pct_change", ()), ("nsmallest", (1, ["a", "b"])), ("tail", 1)]
)
def test_apply_str_axis_1_raises(how, args):
# GH 39211 - some ops don't support axis=1
df = DataFrame({"a": [1, 2], "b": [3, 4]})
msg = f"Operation {how} does not support axis=1"
with pytest.raises(ValueError, match=msg):
df.apply(how, axis=1, args=args)


def test_apply_broadcast(float_frame, int_frame_const_col):

# scalars
Expand Down Expand Up @@ -256,27 +229,6 @@ def test_apply_broadcast(float_frame, int_frame_const_col):
tm.assert_frame_equal(result, expected)


def test_apply_broadcast_error(int_frame_const_col):
df = int_frame_const_col

# > 1 ndim
msg = "too many dims to broadcast"
with pytest.raises(ValueError, match=msg):
df.apply(
lambda x: np.array([1, 2]).reshape(-1, 2),
axis=1,
result_type="broadcast",
)

# cannot broadcast
msg = "cannot broadcast result"
with pytest.raises(ValueError, match=msg):
df.apply(lambda x: [1, 2], axis=1, result_type="broadcast")

with pytest.raises(ValueError, match=msg):
df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast")


def test_apply_raw(float_frame, mixed_type_frame):
def _assert_raw(x):
assert isinstance(x, np.ndarray)
Expand Down Expand Up @@ -424,71 +376,6 @@ def test_apply_differently_indexed():
tm.assert_frame_equal(result1, expected1)


def test_apply_modify_traceback():
data = DataFrame(
{
"A": [
"foo",
"foo",
"foo",
"foo",
"bar",
"bar",
"bar",
"bar",
"foo",
"foo",
"foo",
],
"B": [
"one",
"one",
"one",
"two",
"one",
"one",
"one",
"two",
"two",
"two",
"one",
],
"C": [
"dull",
"dull",
"shiny",
"dull",
"dull",
"shiny",
"shiny",
"dull",
"shiny",
"shiny",
"shiny",
],
"D": np.random.randn(11),
"E": np.random.randn(11),
"F": np.random.randn(11),
}
)

data.loc[4, "C"] = np.nan

def transform(row):
if row["C"].startswith("shin") and row["A"] == "foo":
row["D"] = 7
return row

def transform2(row):
if notna(row["C"]) and row["C"].startswith("shin") and row["A"] == "foo":
row["D"] = 7
return row

msg = "'float' object has no attribute 'startswith'"
with pytest.raises(AttributeError, match=msg):
data.apply(transform, axis=1)


def test_apply_bug():

# GH 6125
Expand Down Expand Up @@ -1101,19 +988,6 @@ def test_result_type(int_frame_const_col):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("result_type", ["foo", 1])
def test_result_type_error(result_type, int_frame_const_col):
# allowed result_type
df = int_frame_const_col

msg = (
"invalid value for result_type, must be one of "
"{None, 'reduce', 'broadcast', 'expand'}"
)
with pytest.raises(ValueError, match=msg):
df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type)


@pytest.mark.parametrize(
"box",
[lambda x: list(x), lambda x: tuple(x), lambda x: np.array(x, dtype="int64")],
Expand Down Expand Up @@ -1170,20 +1044,6 @@ def test_agg_transform(axis, float_frame):
tm.assert_frame_equal(result, expected)


def test_transform_and_agg_err(axis, float_frame):
# cannot both transform and agg
msg = "cannot combine transform and aggregation operations"
with pytest.raises(ValueError, match=msg):
with np.errstate(all="ignore"):
float_frame.agg(["max", "sqrt"], axis=axis)

df = DataFrame({"A": range(5), "B": 5})

def f():
with np.errstate(all="ignore"):
df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis)


def test_demo():
# demonstration tests
df = DataFrame({"A": range(5), "B": 5})
Expand Down Expand Up @@ -1254,16 +1114,6 @@ def test_agg_multiple_mixed_no_warning():
tm.assert_frame_equal(result, expected)


def test_agg_dict_nested_renaming_depr():

df = DataFrame({"A": range(5), "B": 5})

# nested renaming
msg = r"nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
df.agg({"A": {"foo": "min"}, "B": {"bar": "max"}})


def test_agg_reduce(axis, float_frame):
other_axis = 1 if axis in {0, "index"} else 0
name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values()
Expand Down Expand Up @@ -1516,19 +1366,6 @@ def test_agg_cython_table_transform(df, func, expected, axis):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"df, func, expected",
tm.get_cython_table_params(
DataFrame([["a", "b"], ["b", "a"]]), [["cumprod", TypeError]]
),
)
def test_agg_cython_table_raises(df, func, expected, axis):
# GH 21224
msg = "can't multiply sequence by non-int of type 'str'"
with pytest.raises(expected, match=msg):
df.agg(func, axis=axis)


@pytest.mark.parametrize("axis", [0, 1])
@pytest.mark.parametrize(
"args, kwargs",
Expand Down
10 changes: 0 additions & 10 deletions pandas/tests/apply/test_frame_apply_relabeling.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest

import pandas as pd
import pandas._testing as tm
Expand Down Expand Up @@ -96,12 +95,3 @@ def test_agg_namedtuple():
index=pd.Index(["foo", "bar", "cat"]),
)
tm.assert_frame_equal(result, expected)


def test_agg_raises():
# GH 26513
df = pd.DataFrame({"A": [0, 1], "B": [1, 2]})
msg = "Must provide"

with pytest.raises(TypeError, match=msg):
df.agg()
67 changes: 0 additions & 67 deletions pandas/tests/apply/test_frame_transform.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import operator
import re

import numpy as np
import pytest
Expand All @@ -10,7 +9,6 @@
Series,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError
from pandas.core.groupby.base import transformation_kernels
from pandas.tests.frame.common import zip_frames

Expand Down Expand Up @@ -159,47 +157,6 @@ def test_transform_method_name(method):
tm.assert_frame_equal(result, expected)


def test_transform_and_agg_err(axis, float_frame):
# GH 35964
# cannot both transform and agg
msg = "Function did not transform"
with pytest.raises(ValueError, match=msg):
float_frame.transform(["max", "min"], axis=axis)

msg = "Function did not transform"
with pytest.raises(ValueError, match=msg):
float_frame.transform(["max", "sqrt"], axis=axis)


def test_agg_dict_nested_renaming_depr():
df = DataFrame({"A": range(5), "B": 5})

# nested renaming
msg = r"nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
# mypy identifies the argument as an invalid type
df.transform({"A": {"foo": "min"}, "B": {"bar": "max"}})


def test_transform_reducer_raises(all_reductions, frame_or_series):
# GH 35964
op = all_reductions

obj = DataFrame({"A": [1, 2, 3]})
if frame_or_series is not DataFrame:
obj = obj["A"]

msg = "Function did not transform"
with pytest.raises(ValueError, match=msg):
obj.transform(op)
with pytest.raises(ValueError, match=msg):
obj.transform([op])
with pytest.raises(ValueError, match=msg):
obj.transform({"A": op})
with pytest.raises(ValueError, match=msg):
obj.transform({"A": [op]})


wont_fail = ["ffill", "bfill", "fillna", "pad", "backfill", "shift"]
frame_kernels_raise = [x for x in frame_kernels if x not in wont_fail]

Expand Down Expand Up @@ -267,30 +224,6 @@ def f(x, a, b, c):
frame_or_series([1]).transform(f, 0, *expected_args, **expected_kwargs)


def test_transform_missing_columns(axis):
# GH#35964
df = DataFrame({"A": [1, 2], "B": [3, 4]})
match = re.escape("Column(s) ['C'] do not exist")
with pytest.raises(KeyError, match=match):
df.transform({"C": "cumsum"})


def test_transform_none_to_type():
# GH#34377
df = DataFrame({"a": [None]})
msg = "Transform function failed"
with pytest.raises(ValueError, match=msg):
df.transform({"a": int})


def test_transform_mixed_column_name_dtypes():
# GH39025
df = DataFrame({"a": ["1"]})
msg = r"Column\(s\) \[1, 'b'\] do not exist"
with pytest.raises(KeyError, match=msg):
df.transform({"a": int, 1: str, "b": int})


def test_transform_empty_dataframe():
# https://github.com/pandas-dev/pandas/issues/39636
df = DataFrame([], columns=["col1", "col2"])
Expand Down
Loading