Skip to content

Commit d418235

Browse files
committed
TST: add np.ndarray and dict guard tests for DataFrame.at/iat list-like setitem (GH#61223)
1 parent 3173800 commit d418235

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

pandas/tests/indexing/test_at.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,35 @@ def test_at_setitem_list_like_multiindex_row(self):
362362
df.at[("r1", 0), "A"] = [10, 20]
363363
assert df.at[("r1", 0), "A"] == [10, 20]
364364
assert df.at[("r2", 1), "A"] == 2
365+
366+
def test_at_setitem_ndarray_new_column(self):
367+
# GH#61223: np.ndarray should be stored as a single cell in a new column
368+
df = DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"])
369+
arr = np.array([10, 20, 30])
370+
df.at["a", "B"] = arr
371+
stored = df.at["a", "B"]
372+
assert isinstance(stored, np.ndarray)
373+
tm.assert_numpy_array_equal(stored, arr)
374+
assert df["B"].dtype == np.dtype("object")
375+
assert df.at["b", "B"] is np.nan
376+
377+
def test_at_setitem_ndarray_existing_column(self):
378+
# GH#61223: np.ndarray should be stored as a single cell even when the
379+
# existing column has a non-object dtype (column is cast to object)
380+
df = DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"])
381+
arr = np.array([100, 200])
382+
df.at["b", "A"] = arr
383+
stored = df.at["b", "A"]
384+
assert isinstance(stored, np.ndarray)
385+
tm.assert_numpy_array_equal(stored, arr)
386+
assert df["A"].dtype == np.dtype("object")
387+
assert df.at["a", "A"] == 1
388+
assert df.at["c", "A"] == 3
389+
390+
def test_at_setitem_dict_value_not_affected(self):
391+
# GH#61223: dict values are excluded from the list-like single-cell path;
392+
# assigning a dict via .at should still raise ValueError as before
393+
# (existing behaviour must not change)
394+
df = DataFrame({"A": [1, 2, 3]}, index=["a", "b", "c"])
395+
with pytest.raises(ValueError, match="Incompatible indexer"):
396+
df.at["a", "A"] = {"key": "val"}

0 commit comments

Comments
 (0)