Skip to content

Commit

Permalink
TST/CLN: Move raising apply tests to test_invalid_arg (pandas-dev#40213)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Mar 4, 2021
1 parent 74a6898 commit ad2ff04
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
9 changes: 0 additions & 9 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ def test_apply(float_frame):
assert result[d] == expected
assert result.index is float_frame.index

# invalid axis
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"])
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.apply(lambda x: x, 2)

# GH 9573
df = DataFrame({"c0": ["A", "A", "B", "B"], "c1": ["C", "C", "D", "D"]})
result = df.apply(lambda ts: ts.astype("category"))
Expand Down Expand Up @@ -579,9 +573,6 @@ def test_applymap_na_ignore(float_frame):
strlen_frame_with_na[mask] = pd.NA
tm.assert_frame_equal(strlen_frame_na_ignore, strlen_frame_with_na)

with pytest.raises(ValueError, match="na_action must be .*Got 'abc'"):
float_frame_with_na.applymap(lambda x: len(str(x)), na_action="abc")


def test_applymap_box_timestamps():
# GH 2689, GH 2627
Expand Down
79 changes: 78 additions & 1 deletion pandas/tests/apply/test_invalid_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
# 4. invalid result shape/type
# If your test does not fit into one of these categories, add to this list.

from itertools import chain
import re

import numpy as np
import pytest

from pandas import (
Categorical,
DataFrame,
Series,
date_range,
Expand All @@ -34,6 +36,19 @@ def test_result_type_error(result_type, int_frame_const_col):
df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type)


def test_apply_invalid_axis_value():
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"])
msg = "No axis named 2 for object type DataFrame"
with pytest.raises(ValueError, match=msg):
df.apply(lambda x: x, 2)


def test_applymap_invalid_na_action(float_frame):
# GH 23803
with pytest.raises(ValueError, match="na_action must be .*Got 'abc'"):
float_frame.applymap(lambda x: len(str(x)), na_action="abc")


def test_agg_raises():
# GH 26513
df = DataFrame({"A": [0, 1], "B": [1, 2]})
Expand All @@ -43,6 +58,28 @@ def test_agg_raises():
df.agg()


def test_map_with_invalid_na_action_raises():
# https://github.com/pandas-dev/pandas/issues/32815
s = Series([1, 2, 3])
msg = "na_action must either be 'ignore' or None"
with pytest.raises(ValueError, match=msg):
s.map(lambda x: x, na_action="____")


def test_map_categorical_na_action():
values = Categorical(list("ABBABCD"), categories=list("DCBA"), ordered=True)
s = Series(values, name="XX", index=list("abcdefg"))
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
s.map(lambda x: x, na_action="ignore")


def test_map_datetimetz_na_action():
values = date_range("2011-01-01", "2011-01-02", freq="H").tz_localize("Asia/Tokyo")
s = Series(values, name="XX")
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
s.map(lambda x: x, na_action="ignore")


@pytest.mark.parametrize("box", [DataFrame, Series])
@pytest.mark.parametrize("method", ["apply", "agg", "transform"])
@pytest.mark.parametrize("func", [{"A": {"B": "sum"}}, {"A": {"B": ["sum"]}}])
Expand All @@ -54,6 +91,22 @@ def test_nested_renamer(box, method, func):
getattr(obj, method)(func)


def test_series_agg_nested_renamer():
s = Series(range(6), dtype="int64", name="series")
msg = "nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
s.agg({"foo": ["min", "max"]})


def test_multiple_aggregators_with_dict_api():

s = Series(range(6), dtype="int64", name="series")
# nested renaming
msg = "nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
s.agg({"foo": ["min", "max"], "bar": ["sum", "mean"]})


def test_transform_nested_renamer():
# GH 35964
match = "nested renamer is not supported"
Expand Down Expand Up @@ -208,13 +261,37 @@ def transform2(row):
DataFrame([["a", "b"], ["b", "a"]]), [["cumprod", TypeError]]
),
)
def test_agg_cython_table_raises(df, func, expected, axis):
def test_agg_cython_table_raises_frame(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(
"series, func, expected",
chain(
tm.get_cython_table_params(
Series("a b c".split()),
[
("mean", TypeError), # mean raises TypeError
("prod", TypeError),
("std", TypeError),
("var", TypeError),
("median", TypeError),
("cumprod", TypeError),
],
)
),
)
def test_agg_cython_table_raises_series(series, func, expected):
# GH21224
msg = r"[Cc]ould not convert|can't multiply sequence by non-int of type"
with pytest.raises(expected, match=msg):
# e.g. Series('a b'.split()).cumprod() will raise
series.agg(func)


def test_transform_none_to_type():
# GH#34377
df = DataFrame({"a": [None]})
Expand Down
53 changes: 0 additions & 53 deletions pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.base import SpecificationError


def test_series_map_box_timedelta():
Expand Down Expand Up @@ -269,20 +268,6 @@ def test_demo():
expected = Series([0], index=["foo"], name="series")
tm.assert_series_equal(result, expected)

# nested renaming
msg = "nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
s.agg({"foo": ["min", "max"]})


def test_multiple_aggregators_with_dict_api():

s = Series(range(6), dtype="int64", name="series")
# nested renaming
msg = "nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
s.agg({"foo": ["min", "max"], "bar": ["sum", "mean"]})


def test_agg_apply_evaluate_lambdas_the_same(string_series):
# test that we are evaluating row-by-row first
Expand Down Expand Up @@ -439,30 +424,6 @@ def test_agg_cython_table_transform(series, func, expected):
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"series, func, expected",
chain(
tm.get_cython_table_params(
Series("a b c".split()),
[
("mean", TypeError), # mean raises TypeError
("prod", TypeError),
("std", TypeError),
("var", TypeError),
("median", TypeError),
("cumprod", TypeError),
],
)
),
)
def test_agg_cython_table_raises(series, func, expected):
# GH21224
msg = r"[Cc]ould not convert|can't multiply sequence by non-int of type"
with pytest.raises(expected, match=msg):
# e.g. Series('a b'.split()).cumprod() will raise
series.agg(func)


def test_series_apply_no_suffix_index():
# GH36189
s = Series([4] * 3)
Expand Down Expand Up @@ -732,9 +693,6 @@ def test_map_categorical():
tm.assert_series_equal(result, exp)
assert result.dtype == object

with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
s.map(lambda x: x, na_action="ignore")


def test_map_datetimetz():
values = pd.date_range("2011-01-01", "2011-01-02", freq="H").tz_localize(
Expand All @@ -756,9 +714,6 @@ def test_map_datetimetz():
exp = Series(list(range(24)) + [0], name="XX", dtype=np.int64)
tm.assert_series_equal(result, exp)

with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
s.map(lambda x: x, na_action="ignore")

# not vectorized
def f(x):
if not isinstance(x, pd.Timestamp):
Expand Down Expand Up @@ -827,14 +782,6 @@ def test_map_float_to_string_precision():
assert result == expected


def test_map_with_invalid_na_action_raises():
# https://github.com/pandas-dev/pandas/issues/32815
s = Series([1, 2, 3])
msg = "na_action must either be 'ignore' or None"
with pytest.raises(ValueError, match=msg):
s.map(lambda x: x, na_action="____")


def test_apply_to_timedelta():
list_of_valid_strings = ["00:00:01", "00:00:02"]
a = pd.to_timedelta(list_of_valid_strings)
Expand Down

0 comments on commit ad2ff04

Please sign in to comment.