Skip to content

Commit fe643bd

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
TST: collect unary tests (pandas-dev#37230)
1 parent 3a6d742 commit fe643bd

File tree

4 files changed

+180
-171
lines changed

4 files changed

+180
-171
lines changed

pandas/tests/frame/test_operators.py

-118
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,13 @@
1-
from decimal import Decimal
21
import operator
32
import re
43

54
import numpy as np
65
import pytest
76

8-
import pandas as pd
97
from pandas import DataFrame, Series
108
import pandas._testing as tm
119

1210

13-
class TestDataFrameUnaryOperators:
14-
# __pos__, __neg__, __inv__
15-
16-
@pytest.mark.parametrize(
17-
"df,expected",
18-
[
19-
(pd.DataFrame({"a": [-1, 1]}), pd.DataFrame({"a": [1, -1]})),
20-
(pd.DataFrame({"a": [False, True]}), pd.DataFrame({"a": [True, False]})),
21-
(
22-
pd.DataFrame({"a": pd.Series(pd.to_timedelta([-1, 1]))}),
23-
pd.DataFrame({"a": pd.Series(pd.to_timedelta([1, -1]))}),
24-
),
25-
],
26-
)
27-
def test_neg_numeric(self, df, expected):
28-
tm.assert_frame_equal(-df, expected)
29-
tm.assert_series_equal(-df["a"], expected["a"])
30-
31-
@pytest.mark.parametrize(
32-
"df, expected",
33-
[
34-
(np.array([1, 2], dtype=object), np.array([-1, -2], dtype=object)),
35-
([Decimal("1.0"), Decimal("2.0")], [Decimal("-1.0"), Decimal("-2.0")]),
36-
],
37-
)
38-
def test_neg_object(self, df, expected):
39-
# GH#21380
40-
df = pd.DataFrame({"a": df})
41-
expected = pd.DataFrame({"a": expected})
42-
tm.assert_frame_equal(-df, expected)
43-
tm.assert_series_equal(-df["a"], expected["a"])
44-
45-
@pytest.mark.parametrize(
46-
"df",
47-
[
48-
pd.DataFrame({"a": ["a", "b"]}),
49-
pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])}),
50-
],
51-
)
52-
def test_neg_raises(self, df):
53-
msg = (
54-
"bad operand type for unary -: 'str'|"
55-
r"Unary negative expects numeric dtype, not datetime64\[ns\]"
56-
)
57-
with pytest.raises(TypeError, match=msg):
58-
(-df)
59-
with pytest.raises(TypeError, match=msg):
60-
(-df["a"])
61-
62-
def test_invert(self, float_frame):
63-
df = float_frame
64-
65-
tm.assert_frame_equal(-(df < 0), ~(df < 0))
66-
67-
def test_invert_mixed(self):
68-
shape = (10, 5)
69-
df = pd.concat(
70-
[
71-
pd.DataFrame(np.zeros(shape, dtype="bool")),
72-
pd.DataFrame(np.zeros(shape, dtype=int)),
73-
],
74-
axis=1,
75-
ignore_index=True,
76-
)
77-
result = ~df
78-
expected = pd.concat(
79-
[
80-
pd.DataFrame(np.ones(shape, dtype="bool")),
81-
pd.DataFrame(-np.ones(shape, dtype=int)),
82-
],
83-
axis=1,
84-
ignore_index=True,
85-
)
86-
tm.assert_frame_equal(result, expected)
87-
88-
@pytest.mark.parametrize(
89-
"df",
90-
[
91-
pd.DataFrame({"a": [-1, 1]}),
92-
pd.DataFrame({"a": [False, True]}),
93-
pd.DataFrame({"a": pd.Series(pd.to_timedelta([-1, 1]))}),
94-
],
95-
)
96-
def test_pos_numeric(self, df):
97-
# GH#16073
98-
tm.assert_frame_equal(+df, df)
99-
tm.assert_series_equal(+df["a"], df["a"])
100-
101-
@pytest.mark.parametrize(
102-
"df",
103-
[
104-
# numpy changing behavior in the future
105-
pytest.param(
106-
pd.DataFrame({"a": ["a", "b"]}),
107-
marks=[pytest.mark.filterwarnings("ignore")],
108-
),
109-
pd.DataFrame({"a": np.array([-1, 2], dtype=object)}),
110-
pd.DataFrame({"a": [Decimal("-1.0"), Decimal("2.0")]}),
111-
],
112-
)
113-
def test_pos_object(self, df):
114-
# GH#21380
115-
tm.assert_frame_equal(+df, df)
116-
tm.assert_series_equal(+df["a"], df["a"])
117-
118-
@pytest.mark.parametrize(
119-
"df", [pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])})]
120-
)
121-
def test_pos_raises(self, df):
122-
msg = "Unary plus expects .* dtype, not datetime64\\[ns\\]"
123-
with pytest.raises(TypeError, match=msg):
124-
(+df)
125-
with pytest.raises(TypeError, match=msg):
126-
(+df["a"])
127-
128-
12911
class TestDataFrameLogicalOperators:
13012
# &, |, ^
13113

pandas/tests/frame/test_unary.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
from decimal import Decimal
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas as pd
7+
import pandas._testing as tm
8+
9+
10+
class TestDataFrameUnaryOperators:
11+
# __pos__, __neg__, __inv__
12+
13+
@pytest.mark.parametrize(
14+
"df,expected",
15+
[
16+
(pd.DataFrame({"a": [-1, 1]}), pd.DataFrame({"a": [1, -1]})),
17+
(pd.DataFrame({"a": [False, True]}), pd.DataFrame({"a": [True, False]})),
18+
(
19+
pd.DataFrame({"a": pd.Series(pd.to_timedelta([-1, 1]))}),
20+
pd.DataFrame({"a": pd.Series(pd.to_timedelta([1, -1]))}),
21+
),
22+
],
23+
)
24+
def test_neg_numeric(self, df, expected):
25+
tm.assert_frame_equal(-df, expected)
26+
tm.assert_series_equal(-df["a"], expected["a"])
27+
28+
@pytest.mark.parametrize(
29+
"df, expected",
30+
[
31+
(np.array([1, 2], dtype=object), np.array([-1, -2], dtype=object)),
32+
([Decimal("1.0"), Decimal("2.0")], [Decimal("-1.0"), Decimal("-2.0")]),
33+
],
34+
)
35+
def test_neg_object(self, df, expected):
36+
# GH#21380
37+
df = pd.DataFrame({"a": df})
38+
expected = pd.DataFrame({"a": expected})
39+
tm.assert_frame_equal(-df, expected)
40+
tm.assert_series_equal(-df["a"], expected["a"])
41+
42+
@pytest.mark.parametrize(
43+
"df",
44+
[
45+
pd.DataFrame({"a": ["a", "b"]}),
46+
pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])}),
47+
],
48+
)
49+
def test_neg_raises(self, df):
50+
msg = (
51+
"bad operand type for unary -: 'str'|"
52+
r"Unary negative expects numeric dtype, not datetime64\[ns\]"
53+
)
54+
with pytest.raises(TypeError, match=msg):
55+
(-df)
56+
with pytest.raises(TypeError, match=msg):
57+
(-df["a"])
58+
59+
def test_invert(self, float_frame):
60+
df = float_frame
61+
62+
tm.assert_frame_equal(-(df < 0), ~(df < 0))
63+
64+
def test_invert_mixed(self):
65+
shape = (10, 5)
66+
df = pd.concat(
67+
[
68+
pd.DataFrame(np.zeros(shape, dtype="bool")),
69+
pd.DataFrame(np.zeros(shape, dtype=int)),
70+
],
71+
axis=1,
72+
ignore_index=True,
73+
)
74+
result = ~df
75+
expected = pd.concat(
76+
[
77+
pd.DataFrame(np.ones(shape, dtype="bool")),
78+
pd.DataFrame(-np.ones(shape, dtype=int)),
79+
],
80+
axis=1,
81+
ignore_index=True,
82+
)
83+
tm.assert_frame_equal(result, expected)
84+
85+
@pytest.mark.parametrize(
86+
"df",
87+
[
88+
pd.DataFrame({"a": [-1, 1]}),
89+
pd.DataFrame({"a": [False, True]}),
90+
pd.DataFrame({"a": pd.Series(pd.to_timedelta([-1, 1]))}),
91+
],
92+
)
93+
def test_pos_numeric(self, df):
94+
# GH#16073
95+
tm.assert_frame_equal(+df, df)
96+
tm.assert_series_equal(+df["a"], df["a"])
97+
98+
@pytest.mark.parametrize(
99+
"df",
100+
[
101+
# numpy changing behavior in the future
102+
pytest.param(
103+
pd.DataFrame({"a": ["a", "b"]}),
104+
marks=[pytest.mark.filterwarnings("ignore")],
105+
),
106+
pd.DataFrame({"a": np.array([-1, 2], dtype=object)}),
107+
pd.DataFrame({"a": [Decimal("-1.0"), Decimal("2.0")]}),
108+
],
109+
)
110+
def test_pos_object(self, df):
111+
# GH#21380
112+
tm.assert_frame_equal(+df, df)
113+
tm.assert_series_equal(+df["a"], df["a"])
114+
115+
@pytest.mark.parametrize(
116+
"df", [pd.DataFrame({"a": pd.to_datetime(["2017-01-22", "1970-01-01"])})]
117+
)
118+
def test_pos_raises(self, df):
119+
msg = "Unary plus expects .* dtype, not datetime64\\[ns\\]"
120+
with pytest.raises(TypeError, match=msg):
121+
(+df)
122+
with pytest.raises(TypeError, match=msg):
123+
(+df["a"])

pandas/tests/series/test_operators.py

-53
Original file line numberDiff line numberDiff line change
@@ -473,56 +473,3 @@ def test_logical_ops_df_compat(self):
473473

474474
tm.assert_frame_equal(s3.to_frame() | s4.to_frame(), exp_or1.to_frame())
475475
tm.assert_frame_equal(s4.to_frame() | s3.to_frame(), exp_or.to_frame())
476-
477-
478-
class TestSeriesUnaryOps:
479-
# __neg__, __pos__, __inv__
480-
481-
def test_neg(self):
482-
ser = tm.makeStringSeries()
483-
ser.name = "series"
484-
tm.assert_series_equal(-ser, -1 * ser)
485-
486-
def test_invert(self):
487-
ser = tm.makeStringSeries()
488-
ser.name = "series"
489-
tm.assert_series_equal(-(ser < 0), ~(ser < 0))
490-
491-
@pytest.mark.parametrize(
492-
"source, target",
493-
[
494-
([1, 2, 3], [-1, -2, -3]),
495-
([1, 2, None], [-1, -2, None]),
496-
([-1, 0, 1], [1, 0, -1]),
497-
],
498-
)
499-
def test_unary_minus_nullable_int(
500-
self, any_signed_nullable_int_dtype, source, target
501-
):
502-
dtype = any_signed_nullable_int_dtype
503-
s = pd.Series(source, dtype=dtype)
504-
result = -s
505-
expected = pd.Series(target, dtype=dtype)
506-
tm.assert_series_equal(result, expected)
507-
508-
@pytest.mark.parametrize("source", [[1, 2, 3], [1, 2, None], [-1, 0, 1]])
509-
def test_unary_plus_nullable_int(self, any_signed_nullable_int_dtype, source):
510-
dtype = any_signed_nullable_int_dtype
511-
expected = pd.Series(source, dtype=dtype)
512-
result = +expected
513-
tm.assert_series_equal(result, expected)
514-
515-
@pytest.mark.parametrize(
516-
"source, target",
517-
[
518-
([1, 2, 3], [1, 2, 3]),
519-
([1, -2, None], [1, 2, None]),
520-
([-1, 0, 1], [1, 0, 1]),
521-
],
522-
)
523-
def test_abs_nullable_int(self, any_signed_nullable_int_dtype, source, target):
524-
dtype = any_signed_nullable_int_dtype
525-
s = pd.Series(source, dtype=dtype)
526-
result = abs(s)
527-
expected = pd.Series(target, dtype=dtype)
528-
tm.assert_series_equal(result, expected)

pandas/tests/series/test_unary.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pytest
2+
3+
from pandas import Series
4+
import pandas._testing as tm
5+
6+
7+
class TestSeriesUnaryOps:
8+
# __neg__, __pos__, __inv__
9+
10+
def test_neg(self):
11+
ser = tm.makeStringSeries()
12+
ser.name = "series"
13+
tm.assert_series_equal(-ser, -1 * ser)
14+
15+
def test_invert(self):
16+
ser = tm.makeStringSeries()
17+
ser.name = "series"
18+
tm.assert_series_equal(-(ser < 0), ~(ser < 0))
19+
20+
@pytest.mark.parametrize(
21+
"source, target",
22+
[
23+
([1, 2, 3], [-1, -2, -3]),
24+
([1, 2, None], [-1, -2, None]),
25+
([-1, 0, 1], [1, 0, -1]),
26+
],
27+
)
28+
def test_unary_minus_nullable_int(
29+
self, any_signed_nullable_int_dtype, source, target
30+
):
31+
dtype = any_signed_nullable_int_dtype
32+
ser = Series(source, dtype=dtype)
33+
result = -ser
34+
expected = Series(target, dtype=dtype)
35+
tm.assert_series_equal(result, expected)
36+
37+
@pytest.mark.parametrize("source", [[1, 2, 3], [1, 2, None], [-1, 0, 1]])
38+
def test_unary_plus_nullable_int(self, any_signed_nullable_int_dtype, source):
39+
dtype = any_signed_nullable_int_dtype
40+
expected = Series(source, dtype=dtype)
41+
result = +expected
42+
tm.assert_series_equal(result, expected)
43+
44+
@pytest.mark.parametrize(
45+
"source, target",
46+
[
47+
([1, 2, 3], [1, 2, 3]),
48+
([1, -2, None], [1, 2, None]),
49+
([-1, 0, 1], [1, 0, 1]),
50+
],
51+
)
52+
def test_abs_nullable_int(self, any_signed_nullable_int_dtype, source, target):
53+
dtype = any_signed_nullable_int_dtype
54+
ser = Series(source, dtype=dtype)
55+
result = abs(ser)
56+
expected = Series(target, dtype=dtype)
57+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)