Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify check
  • Loading branch information
jbrockmendel committed May 6, 2023
commit 0770a07c565ace4f78f04bd200039efed456aac2
22 changes: 3 additions & 19 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@
ABCDataFrame,
ABCSeries,
)
from pandas.core.dtypes.missing import (
infer_fill_value,
is_valid_na_for_dtype,
isna,
na_value_for_dtype,
)
from pandas.core.dtypes.missing import infer_fill_value

from pandas.core import algorithms as algos
import pandas.core.common as com
Expand Down Expand Up @@ -2091,25 +2086,14 @@ def _setitem_with_indexer_missing(self, indexer, value):
return self._setitem_with_indexer(new_indexer, value, "loc")

# this preserves dtype of the value and of the object
new_dtype = None
if is_list_like(value):
Copy link
Member

Choose a reason for hiding this comment

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

Note for ArrowDtype with pa.list_ type, we would want to treat value like a scalar e.g

ser = pd.Series([[1, 1]], dtype=pd.ArrowDtype(pa.list_(pa.int64()))) ser[0] = [1, 2] 
Copy link
Member Author

Choose a reason for hiding this comment

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

yah, getting rid of this is_list_like check causes us to incorrectly raise on numpy non-object cases when using a list value (for which we don't have any tests). Can fix that in this PR or separately, as it is a bit more invasive.

new_dtype = None

elif is_valid_na_for_dtype(value, self.obj.dtype):
if not is_object_dtype(self.obj.dtype):
# Every NA value is suitable for object, no conversion needed
value = na_value_for_dtype(self.obj.dtype, compat=False)

new_dtype = maybe_promote(self.obj.dtype, value)[0]

elif isna(value):
new_dtype = None
pass
elif not self.obj.empty and not is_object_dtype(self.obj.dtype):
# We should not cast, if we have object dtype because we can
# set timedeltas into object series
curr_dtype = self.obj.dtype
new_dtype, value = maybe_promote(curr_dtype, value)
else:
new_dtype = None

new_values = Series([value], dtype=new_dtype)._values

Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,11 @@ def test_setitem_enlargement_object_none(self, nulls_fixture):
ser[3] = nulls_fixture
expected = Series(["a", "b", nulls_fixture], index=[0, 1, 3])
tm.assert_series_equal(ser, expected)
assert ser[3] is nulls_fixture
if isinstance(nulls_fixture, float):
# We retain the same type, but maybe not the same _object_
assert np.isnan(ser[3])
else:
assert ser[3] is nulls_fixture


def test_setitem_scalar_into_readonly_backing_data():
Expand Down