|
11 | 11 | import pytest
|
12 | 12 |
|
13 | 13 | import pandas as pd
|
14 |
| -from pandas import Index, Series, Timedelta, TimedeltaIndex |
| 14 | +from pandas import Index, Int64Index, Series, Timedelta, TimedeltaIndex, array |
15 | 15 | import pandas._testing as tm
|
16 | 16 | from pandas.core import ops
|
17 | 17 |
|
18 | 18 |
|
| 19 | +@pytest.fixture(params=[Index, Series, tm.to_array]) |
| 20 | +def box_pandas_1d_array(request): |
| 21 | + """ |
| 22 | + Fixture to test behavior for Index, Series and tm.to_array classes |
| 23 | + """ |
| 24 | + return request.param |
| 25 | + |
| 26 | + |
19 | 27 | def adjust_negative_zero(zero, expected):
|
20 | 28 | """
|
21 | 29 | Helper to adjust the expected result if we are dividing by -0.0
|
@@ -1330,3 +1338,33 @@ def test_dataframe_div_silenced():
|
1330 | 1338 | )
|
1331 | 1339 | with tm.assert_produces_warning(None):
|
1332 | 1340 | pdf1.div(pdf2, fill_value=0)
|
| 1341 | + |
| 1342 | + |
| 1343 | +@pytest.mark.parametrize( |
| 1344 | + "data, expected_data", |
| 1345 | + [([0, 1, 2], [0, 2, 4])], |
| 1346 | +) |
| 1347 | +def test_integer_array_add_list_like( |
| 1348 | + box_pandas_1d_array, box_1d_array, data, expected_data |
| 1349 | +): |
| 1350 | + # GH22606 Verify operators with IntegerArray and list-likes |
| 1351 | + arr = array(data, dtype="Int64") |
| 1352 | + container = box_pandas_1d_array(arr) |
| 1353 | + left = container + box_1d_array(data) |
| 1354 | + right = box_1d_array(data) + container |
| 1355 | + |
| 1356 | + if Series == box_pandas_1d_array: |
| 1357 | + assert_function = tm.assert_series_equal |
| 1358 | + expected = Series(expected_data, dtype="Int64") |
| 1359 | + elif Series == box_1d_array: |
| 1360 | + assert_function = tm.assert_series_equal |
| 1361 | + expected = Series(expected_data, dtype="object") |
| 1362 | + elif Index in (box_pandas_1d_array, box_1d_array): |
| 1363 | + assert_function = tm.assert_index_equal |
| 1364 | + expected = Int64Index(expected_data) |
| 1365 | + else: |
| 1366 | + assert_function = tm.assert_numpy_array_equal |
| 1367 | + expected = np.array(expected_data, dtype="object") |
| 1368 | + |
| 1369 | + assert_function(left, expected) |
| 1370 | + assert_function(right, expected) |
0 commit comments