Skip to content

Commit 43c0508

Browse files
authored
BUG: iloc.setitem raising NotImplementedError for all null slice with one column df (#47987)
1 parent e7afa4b commit 43c0508

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ Indexing
930930
- Bug in setting a NA value (``None`` or ``np.nan``) into a :class:`Series` with int-based :class:`IntervalDtype` incorrectly casting to object dtype instead of a float-based :class:`IntervalDtype` (:issue:`45568`)
931931
- Bug in indexing setting values into an ``ExtensionDtype`` column with ``df.iloc[:, i] = values`` with ``values`` having the same dtype as ``df.iloc[:, i]`` incorrectly inserting a new array instead of setting in-place (:issue:`33457`)
932932
- Bug in :meth:`Series.__setitem__` with a non-integer :class:`Index` when using an integer key to set a value that cannot be set inplace where a ``ValueError`` was raised instead of casting to a common dtype (:issue:`45070`)
933+
- Bug in :meth:`DataFrame.loc` raising ``NotImplementedError`` when setting value into one column :class:`DataFrame` with all null slice as column indexer (:issue:`45469`)
934+
- Bug in :meth:`DataFrame.loc` not casting ``None`` to ``NA`` when setting value a list into :class:`DataFrame` (:issue:`47987`)
933935
- Bug in :meth:`Series.__setitem__` when setting incompatible values into a ``PeriodDtype`` or ``IntervalDtype`` :class:`Series` raising when indexing with a boolean mask but coercing when indexing with otherwise-equivalent indexers; these now consistently coerce, along with :meth:`Series.mask` and :meth:`Series.where` (:issue:`45768`)
934936
- Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`)
935937
- Bug in :func:`isin` upcasting to ``float64`` with unsigned integer dtype and list-like argument without a dtype (:issue:`46485`)

pandas/core/arrays/string_.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ def __setitem__(self, key, value):
409409
if len(value) and not lib.is_string_array(value, skipna=True):
410410
raise ValueError("Must provide strings.")
411411

412+
value[isna(value)] = libmissing.NA
413+
412414
super().__setitem__(key, value)
413415

414416
def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:

pandas/core/internals/blocks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,10 @@ def _unwrap_setitem_indexer(self, indexer):
17211721
elif lib.is_integer(indexer[1]) and indexer[1] == 0:
17221722
# reached via setitem_single_block passing the whole indexer
17231723
indexer = indexer[0]
1724+
1725+
elif com.is_null_slice(indexer[1]):
1726+
indexer = indexer[0]
1727+
17241728
else:
17251729
raise NotImplementedError(
17261730
"This should not be reached. Please report a bug at "

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,22 @@ def test_loc_internals_not_updated_correctly(self):
13471347
)
13481348
tm.assert_series_equal(result, expected)
13491349

1350+
@pytest.mark.parametrize("val", [None, [None], pd.NA, [pd.NA]])
1351+
def test_iloc_setitem_string_list_na(self, val):
1352+
# GH#45469
1353+
df = DataFrame({"a": ["a", "b", "c"]}, dtype="string")
1354+
df.iloc[[0], :] = val
1355+
expected = DataFrame({"a": [pd.NA, "b", "c"]}, dtype="string")
1356+
tm.assert_frame_equal(df, expected)
1357+
1358+
@pytest.mark.parametrize("val", [None, pd.NA])
1359+
def test_iloc_setitem_string_na(self, val):
1360+
# GH#45469
1361+
df = DataFrame({"a": ["a", "b", "c"]}, dtype="string")
1362+
df.iloc[0, :] = val
1363+
expected = DataFrame({"a": [pd.NA, "b", "c"]}, dtype="string")
1364+
tm.assert_frame_equal(df, expected)
1365+
13501366

13511367
class TestDataFrameIndexingUInt64:
13521368
def test_setitem(self, uint64_frame):

0 commit comments

Comments
 (0)