|
4 | 4 | """
|
5 | 5 |
|
6 | 6 | import operator
|
| 7 | +from re import escape |
7 | 8 |
|
8 | 9 | import numpy as np
|
9 | 10 | import pytest
|
@@ -249,6 +250,32 @@ def test_mul(dtype):
|
249 | 250 | tm.assert_extension_array_equal(result, expected)
|
250 | 251 |
|
251 | 252 |
|
| 253 | +def test_add_series(dtype): |
| 254 | + arr = pd.array(["a", "b", "c", "d"], dtype=dtype) |
| 255 | + df = pd.Series(["t", "y", "v", "w"], dtype=object) |
| 256 | + |
| 257 | + result = arr + df |
| 258 | + expected = pd.Series(["at", "by", "cv", "dw"]).astype(dtype) |
| 259 | + tm.assert_series_equal(result, expected) |
| 260 | + |
| 261 | + result = df + arr |
| 262 | + expected = pd.Series(["ta", "yb", "vc", "wd"]).astype(dtype) |
| 263 | + tm.assert_series_equal(result, expected) |
| 264 | + |
| 265 | + |
| 266 | +def test_add_series_float(dtype): |
| 267 | + arr = pd.array(["a", "b", "c", "d"], dtype=dtype) |
| 268 | + df = pd.Series([1, 2.0, 3.5, 4]) |
| 269 | + |
| 270 | + result = arr + df |
| 271 | + expected = pd.Series(["a1", "b2", "c3.5", "d4"]).astype(dtype) |
| 272 | + tm.assert_series_equal(result, expected) |
| 273 | + |
| 274 | + result = df + arr |
| 275 | + expected = pd.Series(["1a", "2b", "3.5c", "4d"]).astype(dtype) |
| 276 | + tm.assert_series_equal(result, expected) |
| 277 | + |
| 278 | + |
252 | 279 | def test_add_strings(dtype):
|
253 | 280 | arr = pd.array(["a", "b", "c", "d"], dtype=dtype)
|
254 | 281 | df = pd.DataFrame([["t", "y", "v", "w"]], dtype=object)
|
@@ -278,19 +305,58 @@ def test_add_frame(dtype):
|
278 | 305 | tm.assert_frame_equal(result, expected, check_dtype=False)
|
279 | 306 |
|
280 | 307 |
|
281 |
| -def test_add_frame_mixed_type(dtype): |
282 |
| - arr = pd.array(["a", "bc", 3, np.nan], dtype=dtype) |
283 |
| - df = pd.DataFrame([[1, 2, 3.3, 4]]) |
| 308 | +def test_add_frame_int(dtype): |
| 309 | + arr = pd.array(["a", "b", "c", np.nan], dtype=dtype) |
| 310 | + df = pd.DataFrame([[1, np.nan, 3, np.nan]]) |
284 | 311 |
|
285 | 312 | result = arr + df
|
286 |
| - expected = pd.DataFrame([["a1", "bc2", "33.3", np.nan]]).astype(dtype) |
| 313 | + expected = pd.DataFrame([["a1", np.nan, "c3", np.nan]]).astype(dtype) |
287 | 314 | tm.assert_frame_equal(result, expected, check_dtype=False)
|
288 | 315 |
|
289 | 316 | result = df + arr
|
290 |
| - expected = pd.DataFrame([["1a", "2bc", "3.33", np.nan]]).astype(dtype) |
| 317 | + expected = pd.DataFrame([["1a", np.nan, "3c", np.nan]]).astype(dtype) |
291 | 318 | tm.assert_frame_equal(result, expected, check_dtype=False)
|
292 | 319 |
|
293 | 320 |
|
| 321 | +@pytest.mark.parametrize( |
| 322 | + "invalid", |
| 323 | + [ |
| 324 | + pd.Timedelta(hours=31), |
| 325 | + pd.Timestamp("2021-01-01"), |
| 326 | + np.datetime64("NaT", "ns"), |
| 327 | + pd.NaT, |
| 328 | + True, |
| 329 | + pd.Period("2025-09"), |
| 330 | + pd.Categorical(["test"]), |
| 331 | + pd.offsets.Minute(3), |
| 332 | + pd.Interval(1, 2, closed="right"), |
| 333 | + ], |
| 334 | +) |
| 335 | +def test_add_frame_invalid(dtype, invalid): |
| 336 | + arr = pd.array(["a", np.nan], dtype=dtype) |
| 337 | + df = pd.DataFrame([[invalid, invalid]]) |
| 338 | + |
| 339 | + if dtype.storage == "pyarrow": |
| 340 | + if invalid == pd.Categorical(["test"]): |
| 341 | + msg = ( |
| 342 | + "Incompatible type found when converting " |
| 343 | + "to PyArrow dtype for operation." |
| 344 | + ) |
| 345 | + else: |
| 346 | + msg = ( |
| 347 | + "Can only add string arrays to dtypes " |
| 348 | + "null, int, float, str, and binary." |
| 349 | + ) |
| 350 | + with pytest.raises(TypeError, match=msg): |
| 351 | + arr + df |
| 352 | + else: |
| 353 | + msg = escape( |
| 354 | + "Only supports op(add) between StringArray and dtypes int, float, and str." |
| 355 | + ) |
| 356 | + with pytest.raises(TypeError, match=msg): |
| 357 | + arr + df |
| 358 | + |
| 359 | + |
294 | 360 | def test_comparison_methods_scalar(comparison_op, dtype):
|
295 | 361 | op_name = f"__{comparison_op.__name__}__"
|
296 | 362 | a = pd.array(["a", None, "c"], dtype=dtype)
|
|
0 commit comments