Skip to content

BUG: Series add casting to object for list and masked series #50762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ Numeric
- Bug in arithmetic operations on :class:`Series` not propagating mask when combining masked dtypes and numpy dtypes (:issue:`45810`, :issue:`42630`)
- Bug in DataFrame reduction methods (e.g. :meth:`DataFrame.sum`) with object dtype, ``axis=1`` and ``numeric_only=False`` would not be coerced to float (:issue:`49551`)
- Bug in :meth:`DataFrame.sem` and :meth:`Series.sem` where an erroneous ``TypeError`` would always raise when using data backed by an :class:`ArrowDtype` (:issue:`49759`)
- Bug in :meth:`Series.__add__` casting to object for list and masked :class:`Series` (:issue:`22962`)

Conversion
^^^^^^^^^^
Expand Down
21 changes: 21 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,27 @@ def _arith_method(self, other, op):
op_name = op.__name__
omask = None

if (
not hasattr(other, "dtype")
and is_list_like(other)
and len(other) == len(self)
):
# Try inferring masked dtype instead of casting to object
inferred_dtype = lib.infer_dtype(other, skipna=True)
if inferred_dtype == "integer":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about boolean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, adjusted

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason not to call pd.array here?

context: im looking at issues relating to infer_dtype and trying to track down how many of our usages we really need

from pandas.core.arrays import IntegerArray

other = IntegerArray._from_sequence(other)
elif inferred_dtype in ["floating", "mixed-integer-float"]:
from pandas.core.arrays import FloatingArray

other = FloatingArray._from_sequence(other)

elif inferred_dtype in ["boolean"]:
from pandas.core.arrays import BooleanArray

other = BooleanArray._from_sequence(other)

if isinstance(other, BaseMaskedArray):
other, omask = other._data, other._mask

Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,27 @@ def test_mask_div_propagate_na_for_non_na_dtype(self):
result = ser2 / ser1
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("val, dtype", [(3, "Int64"), (3.5, "Float64")])
def test_add_list_to_masked_array(self, val, dtype):
# GH#22962
ser = Series([1, None, 3], dtype="Int64")
result = ser + [1, None, val]
expected = Series([2, None, 3 + val], dtype=dtype)
tm.assert_series_equal(result, expected)

result = [1, None, val] + ser
tm.assert_series_equal(result, expected)

def test_add_list_to_masked_array_boolean(self):
# GH#22962
ser = Series([True, None, False], dtype="boolean")
result = ser + [True, None, True]
expected = Series([True, None, True], dtype="boolean")
tm.assert_series_equal(result, expected)

result = [True, None, True] + ser
tm.assert_series_equal(result, expected)


# ------------------------------------------------------------------
# Comparisons
Expand Down