File size: 10,554 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
from functools import partial
import re
import numpy as np
import pytest
import pandas as pd
import pandas._testing as tm
from pandas.api.types import is_extension_array_dtype
dtypes = [
"int64",
"Int64",
{"A": "int64", "B": "Int64"},
]
@pytest.mark.parametrize("dtype", dtypes)
def test_unary_unary(dtype):
# unary input, unary output
values = np.array([[-1, -1], [1, 1]], dtype="int64")
df = pd.DataFrame(values, columns=["A", "B"], index=["a", "b"]).astype(dtype=dtype)
result = np.positive(df)
expected = pd.DataFrame(
np.positive(values), index=df.index, columns=df.columns
).astype(dtype)
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("dtype", dtypes)
def test_unary_binary(request, dtype):
# unary input, binary output
if is_extension_array_dtype(dtype) or isinstance(dtype, dict):
request.applymarker(
pytest.mark.xfail(
reason="Extension / mixed with multiple outputs not implemented."
)
)
values = np.array([[-1, -1], [1, 1]], dtype="int64")
df = pd.DataFrame(values, columns=["A", "B"], index=["a", "b"]).astype(dtype=dtype)
result_pandas = np.modf(df)
assert isinstance(result_pandas, tuple)
assert len(result_pandas) == 2
expected_numpy = np.modf(values)
for result, b in zip(result_pandas, expected_numpy):
expected = pd.DataFrame(b, index=df.index, columns=df.columns)
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("dtype", dtypes)
def test_binary_input_dispatch_binop(dtype):
# binop ufuncs are dispatched to our dunder methods.
values = np.array([[-1, -1], [1, 1]], dtype="int64")
df = pd.DataFrame(values, columns=["A", "B"], index=["a", "b"]).astype(dtype=dtype)
result = np.add(df, df)
expected = pd.DataFrame(
np.add(values, values), index=df.index, columns=df.columns
).astype(dtype)
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize(
"func,arg,expected",
[
(np.add, 1, [2, 3, 4, 5]),
(
partial(np.add, where=[[False, True], [True, False]]),
np.array([[1, 1], [1, 1]]),
[0, 3, 4, 0],
),
(np.power, np.array([[1, 1], [2, 2]]), [1, 2, 9, 16]),
(np.subtract, 2, [-1, 0, 1, 2]),
(
partial(np.negative, where=np.array([[False, True], [True, False]])),
None,
[0, -2, -3, 0],
),
],
)
def test_ufunc_passes_args(func, arg, expected):
# GH#40662
arr = np.array([[1, 2], [3, 4]])
df = pd.DataFrame(arr)
result_inplace = np.zeros_like(arr)
# 1-argument ufunc
if arg is None:
result = func(df, out=result_inplace)
else:
result = func(df, arg, out=result_inplace)
expected = np.array(expected).reshape(2, 2)
tm.assert_numpy_array_equal(result_inplace, expected)
expected = pd.DataFrame(expected)
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("dtype_a", dtypes)
@pytest.mark.parametrize("dtype_b", dtypes)
def test_binary_input_aligns_columns(request, dtype_a, dtype_b):
if (
is_extension_array_dtype(dtype_a)
or isinstance(dtype_a, dict)
or is_extension_array_dtype(dtype_b)
or isinstance(dtype_b, dict)
):
request.applymarker(
pytest.mark.xfail(
reason="Extension / mixed with multiple inputs not implemented."
)
)
df1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]}).astype(dtype_a)
if isinstance(dtype_a, dict) and isinstance(dtype_b, dict):
dtype_b = dtype_b.copy()
dtype_b["C"] = dtype_b.pop("B")
df2 = pd.DataFrame({"A": [1, 2], "C": [3, 4]}).astype(dtype_b)
# As of 2.0, align first before applying the ufunc
result = np.heaviside(df1, df2)
expected = np.heaviside(
np.array([[1, 3, np.nan], [2, 4, np.nan]]),
np.array([[1, np.nan, 3], [2, np.nan, 4]]),
)
expected = pd.DataFrame(expected, index=[0, 1], columns=["A", "B", "C"])
tm.assert_frame_equal(result, expected)
result = np.heaviside(df1, df2.values)
expected = pd.DataFrame([[1.0, 1.0], [1.0, 1.0]], columns=["A", "B"])
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("dtype", dtypes)
def test_binary_input_aligns_index(request, dtype):
if is_extension_array_dtype(dtype) or isinstance(dtype, dict):
request.applymarker(
pytest.mark.xfail(
reason="Extension / mixed with multiple inputs not implemented."
)
)
df1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]}, index=["a", "b"]).astype(dtype)
df2 = pd.DataFrame({"A": [1, 2], "B": [3, 4]}, index=["a", "c"]).astype(dtype)
result = np.heaviside(df1, df2)
expected = np.heaviside(
np.array([[1, 3], [3, 4], [np.nan, np.nan]]),
np.array([[1, 3], [np.nan, np.nan], [3, 4]]),
)
# TODO(FloatArray): this will be Float64Dtype.
expected = pd.DataFrame(expected, index=["a", "b", "c"], columns=["A", "B"])
tm.assert_frame_equal(result, expected)
result = np.heaviside(df1, df2.values)
expected = pd.DataFrame(
[[1.0, 1.0], [1.0, 1.0]], columns=["A", "B"], index=["a", "b"]
)
tm.assert_frame_equal(result, expected)
def test_binary_frame_series_raises():
# We don't currently implement
df = pd.DataFrame({"A": [1, 2]})
with pytest.raises(NotImplementedError, match="logaddexp"):
np.logaddexp(df, df["A"])
with pytest.raises(NotImplementedError, match="logaddexp"):
np.logaddexp(df["A"], df)
def test_unary_accumulate_axis():
# https://github.com/pandas-dev/pandas/issues/39259
df = pd.DataFrame({"a": [1, 3, 2, 4]})
result = np.maximum.accumulate(df)
expected = pd.DataFrame({"a": [1, 3, 3, 4]})
tm.assert_frame_equal(result, expected)
df = pd.DataFrame({"a": [1, 3, 2, 4], "b": [0.1, 4.0, 3.0, 2.0]})
result = np.maximum.accumulate(df)
# in theory could preserve int dtype for default axis=0
expected = pd.DataFrame({"a": [1.0, 3.0, 3.0, 4.0], "b": [0.1, 4.0, 4.0, 4.0]})
tm.assert_frame_equal(result, expected)
result = np.maximum.accumulate(df, axis=0)
tm.assert_frame_equal(result, expected)
result = np.maximum.accumulate(df, axis=1)
expected = pd.DataFrame({"a": [1.0, 3.0, 2.0, 4.0], "b": [1.0, 4.0, 3.0, 4.0]})
tm.assert_frame_equal(result, expected)
def test_frame_outer_disallowed():
df = pd.DataFrame({"A": [1, 2]})
with pytest.raises(NotImplementedError, match=""):
# deprecation enforced in 2.0
np.subtract.outer(df, df)
def test_alignment_deprecation_enforced():
# Enforced in 2.0
# https://github.com/pandas-dev/pandas/issues/39184
df1 = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df2 = pd.DataFrame({"b": [1, 2, 3], "c": [4, 5, 6]})
s1 = pd.Series([1, 2], index=["a", "b"])
s2 = pd.Series([1, 2], index=["b", "c"])
# binary dataframe / dataframe
expected = pd.DataFrame({"a": [2, 4, 6], "b": [8, 10, 12]})
with tm.assert_produces_warning(None):
# aligned -> no warning!
result = np.add(df1, df1)
tm.assert_frame_equal(result, expected)
result = np.add(df1, df2.values)
tm.assert_frame_equal(result, expected)
result = np.add(df1, df2)
expected = pd.DataFrame({"a": [np.nan] * 3, "b": [5, 7, 9], "c": [np.nan] * 3})
tm.assert_frame_equal(result, expected)
result = np.add(df1.values, df2)
expected = pd.DataFrame({"b": [2, 4, 6], "c": [8, 10, 12]})
tm.assert_frame_equal(result, expected)
# binary dataframe / series
expected = pd.DataFrame({"a": [2, 3, 4], "b": [6, 7, 8]})
with tm.assert_produces_warning(None):
# aligned -> no warning!
result = np.add(df1, s1)
tm.assert_frame_equal(result, expected)
result = np.add(df1, s2.values)
tm.assert_frame_equal(result, expected)
expected = pd.DataFrame(
{"a": [np.nan] * 3, "b": [5.0, 6.0, 7.0], "c": [np.nan] * 3}
)
result = np.add(df1, s2)
tm.assert_frame_equal(result, expected)
msg = "Cannot apply ufunc <ufunc 'add'> to mixed DataFrame and Series inputs."
with pytest.raises(NotImplementedError, match=msg):
np.add(s2, df1)
def test_alignment_deprecation_many_inputs_enforced():
# Enforced in 2.0
# https://github.com/pandas-dev/pandas/issues/39184
# test that the deprecation also works with > 2 inputs -> using a numba
# written ufunc for this because numpy itself doesn't have such ufuncs
numba = pytest.importorskip("numba")
@numba.vectorize([numba.float64(numba.float64, numba.float64, numba.float64)])
def my_ufunc(x, y, z):
return x + y + z
df1 = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df2 = pd.DataFrame({"b": [1, 2, 3], "c": [4, 5, 6]})
df3 = pd.DataFrame({"a": [1, 2, 3], "c": [4, 5, 6]})
result = my_ufunc(df1, df2, df3)
expected = pd.DataFrame(np.full((3, 3), np.nan), columns=["a", "b", "c"])
tm.assert_frame_equal(result, expected)
# all aligned -> no warning
with tm.assert_produces_warning(None):
result = my_ufunc(df1, df1, df1)
expected = pd.DataFrame([[3.0, 12.0], [6.0, 15.0], [9.0, 18.0]], columns=["a", "b"])
tm.assert_frame_equal(result, expected)
# mixed frame / arrays
msg = (
r"operands could not be broadcast together with shapes \(3,3\) \(3,3\) \(3,2\)"
)
with pytest.raises(ValueError, match=msg):
my_ufunc(df1, df2, df3.values)
# single frame -> no warning
with tm.assert_produces_warning(None):
result = my_ufunc(df1, df2.values, df3.values)
tm.assert_frame_equal(result, expected)
# takes indices of first frame
msg = (
r"operands could not be broadcast together with shapes \(3,2\) \(3,3\) \(3,3\)"
)
with pytest.raises(ValueError, match=msg):
my_ufunc(df1.values, df2, df3)
def test_array_ufuncs_for_many_arguments():
# GH39853
def add3(x, y, z):
return x + y + z
ufunc = np.frompyfunc(add3, 3, 1)
df = pd.DataFrame([[1, 2], [3, 4]])
result = ufunc(df, df, 1)
expected = pd.DataFrame([[3, 5], [7, 9]], dtype=object)
tm.assert_frame_equal(result, expected)
ser = pd.Series([1, 2])
msg = (
"Cannot apply ufunc <ufunc 'add3 (vectorized)'> "
"to mixed DataFrame and Series inputs."
)
with pytest.raises(NotImplementedError, match=re.escape(msg)):
ufunc(df, df, ser)
|