Skip to content

Commit

Permalink
Backport PR #48619 on branch 1.5.x (REGR: Loc.setitem with enlargemen…
Browse files Browse the repository at this point in the history
…t raises for nested data) (#48629)

Backport PR #48619: REGR: Loc.setitem with enlargement raises for nested data

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Sep 19, 2022
1 parent f83e2fe commit e726483
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,9 @@ 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
if is_valid_na_for_dtype(value, self.obj.dtype):
if not is_scalar(value):
new_dtype = None
elif is_valid_na_for_dtype(value, self.obj.dtype):
value = na_value_for_dtype(self.obj.dtype, compat=False)
new_dtype = maybe_promote(self.obj.dtype, value)[0]
elif isna(value):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ def test_loc_boolean_indexer_miss_matching_index():
ser.loc[indexer]


def test_loc_setitem_nested_data_enlargement():
# GH#48614
df = DataFrame({"a": [1]})
ser = Series({"label": df})
ser.loc["new_label"] = df
expected = Series({"label": df, "new_label": df})
tm.assert_series_equal(ser, expected)


class TestDeprecatedIndexers:
@pytest.mark.parametrize("key", [{1}, {1: 1}])
def test_getitem_dict_and_set_deprecated(self, key):
Expand Down

0 comments on commit e726483

Please sign in to comment.