Skip to content

Commit ba5cb55

Browse files
committed
32-bit compat
1 parent 8a967d2 commit ba5cb55

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

pandas/tests/series/test_ufunc.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import string
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas as pd
7+
import pandas.util.testing as tm
8+
9+
UNARY_UFUNCS = [np.positive, np.floor, np.exp]
10+
BINARY_UFUNCS = [
11+
np.add, # dunder op
12+
np.logaddexp,
13+
]
14+
SPARSE = [
15+
pytest.param(True,
16+
marks=pytest.mark.xfail(reason="Series.__array_ufunc__")),
17+
False,
18+
]
19+
SPARSE_IDS = ['sparse', 'dense']
20+
SHUFFLE = [
21+
pytest.param(True, marks=pytest.mark.xfail(reason="GH-26945")),
22+
False
23+
]
24+
25+
26+
@pytest.fixture
27+
def arrays_for_binary_ufunc():
28+
"""
29+
A pair of random, length-100 integer-dtype arrays, that are mostly 0.
30+
"""
31+
a1 = np.random.randint(0, 10, 100, dtype='int64')
32+
a2 = np.random.randint(0, 10, 100, dtype='int64')
33+
a1[::3] = 0
34+
a2[::4] = 0
35+
return a1, a2
36+
37+
38+
@pytest.mark.parametrize("ufunc", UNARY_UFUNCS)
39+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
40+
def test_unary_ufunc(ufunc, sparse):
41+
array = np.random.randint(0, 10, 10, dtype='int64')
42+
array[::2] = 0
43+
if sparse:
44+
array = pd.SparseArray(array, dtype=pd.SparseDtype('int', 0))
45+
46+
index = list(string.ascii_letters[:10])
47+
name = "name"
48+
series = pd.Series(array, index=index, name=name)
49+
50+
result = ufunc(series)
51+
expected = pd.Series(ufunc(array), index=index, name=name)
52+
tm.assert_series_equal(result, expected)
53+
54+
55+
@pytest.mark.parametrize("ufunc", BINARY_UFUNCS)
56+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
57+
@pytest.mark.parametrize("shuffle", SHUFFLE)
58+
@pytest.mark.parametrize("box_other", ['series', 'index', 'raw'])
59+
@pytest.mark.parametrize("flip", [True, False],
60+
ids=['flipped', 'straight'])
61+
def test_binary_ufunc(ufunc, sparse, shuffle, box_other,
62+
flip,
63+
arrays_for_binary_ufunc):
64+
# Check the invariant that
65+
# ufunc(Series(a), Series(b)) == Series(ufunc(a, b))
66+
# with alignment.
67+
a1, a2 = arrays_for_binary_ufunc
68+
if sparse:
69+
a1 = pd.SparseArray(a1, dtype=pd.SparseDtype('int', 0))
70+
a2 = pd.SparseArray(a2, dtype=pd.SparseDtype('int', 0))
71+
72+
name = "name"
73+
s1 = pd.Series(a1, name=name)
74+
if box_other == 'series':
75+
s2 = pd.Series(a2, name=name)
76+
elif box_other == 'index':
77+
# Index should defer to Series
78+
s2 = pd.Index(a2, naame=name)
79+
else:
80+
s2 = a2
81+
82+
# handle shufling / alignment
83+
# If boxing -- ufunc(series, series) -- then we don't need to shuffle
84+
# the other array for the expected, since we align.
85+
# If not boxing -- ufunc(series, array) -- then we do need to shuffle
86+
# the other array, since we *dont'* align
87+
idx = np.random.permutation(len(s1))
88+
if box_other and shuffle:
89+
# ensure we align before applying the ufunc
90+
s2 = s2.take(idx)
91+
elif shuffle:
92+
a2 = a2.take(idx)
93+
94+
a, b = s1, s2
95+
c, d = a1, a2
96+
97+
if flip:
98+
a, b = b, a
99+
c, d = d, c
100+
101+
result = ufunc(a, b)
102+
expected = pd.Series(ufunc(c, d), name=name)
103+
if box_other == 'index' and flip:
104+
raise pytest.xfail("Index should defer to Series")
105+
tm.assert_series_equal(result, expected)
106+
107+
108+
@pytest.mark.parametrize("ufunc", BINARY_UFUNCS)
109+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
110+
@pytest.mark.parametrize("flip", [True, False])
111+
def test_binary_ufunc_scalar(ufunc, sparse, flip, arrays_for_binary_ufunc):
112+
array, _ = arrays_for_binary_ufunc
113+
if sparse:
114+
array = pd.SparseArray(array)
115+
other = 2
116+
series = pd.Series(array, name="name")
117+
118+
a, b = series, other
119+
c, d = array, other
120+
if flip:
121+
c, d = b, c
122+
a, b = b, a
123+
124+
expected = pd.Series(ufunc(a, b), name="name")
125+
result = pd.Series(ufunc(c, d), name="name")
126+
tm.assert_series_equal(result, expected)
127+
128+
129+
@pytest.mark.parametrize("ufunc", [np.divmod]) # any others?
130+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
131+
@pytest.mark.parametrize("shuffle", SHUFFLE)
132+
@pytest.mark.filterwarnings("ignore:divide by zero:RuntimeWarning")
133+
def test_multiple_ouput_binary_ufuncs(ufunc, sparse, shuffle,
134+
arrays_for_binary_ufunc):
135+
a1, a2 = arrays_for_binary_ufunc
136+
137+
if sparse:
138+
a1 = pd.SparseArray(a1, dtype=pd.SparseDtype('int', 0))
139+
a2 = pd.SparseArray(a2, dtype=pd.SparseDtype('int', 0))
140+
141+
s1 = pd.Series(a1)
142+
s2 = pd.Series(a2)
143+
144+
if shuffle:
145+
# ensure we align before applying the ufunc
146+
s2 = s2.sample(frac=1)
147+
148+
expected = ufunc(a1, a2)
149+
assert isinstance(expected, tuple)
150+
151+
result = ufunc(s1, s2)
152+
assert isinstance(result, tuple)
153+
tm.assert_series_equal(result[0], pd.Series(expected[0]))
154+
tm.assert_series_equal(result[1], pd.Series(expected[1]))
155+
156+
157+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
158+
def test_multiple_ouput_ufunc(sparse, arrays_for_binary_ufunc):
159+
array, _ = arrays_for_binary_ufunc
160+
161+
if sparse:
162+
array = pd.SparseArray(array)
163+
164+
series = pd.Series(array, name="name")
165+
result = np.modf(series)
166+
expected = np.modf(array)
167+
168+
assert isinstance(result, tuple)
169+
assert isinstance(expected, tuple)
170+
171+
tm.assert_series_equal(result[0], pd.Series(expected[0], name="name"))
172+
tm.assert_series_equal(result[1], pd.Series(expected[1], name="name"))
173+
174+
175+
@pytest.mark.parametrize("sparse", SPARSE, ids=SPARSE_IDS)
176+
@pytest.mark.parametrize("ufunc", BINARY_UFUNCS)
177+
@pytest.mark.xfail(reason="Series.__array_ufunc__")
178+
def test_binary_ufunc_drops_series_name(ufunc, sparse,
179+
arrays_for_binary_ufunc):
180+
# Drop the names when they differ.
181+
a1, a2 = arrays_for_binary_ufunc
182+
s1 = pd.Series(a1, name='a')
183+
s2 = pd.Series(a2, name='b')
184+
185+
result = ufunc(s1, s2)
186+
assert result.name is None

0 commit comments

Comments
 (0)