Skip to content

BUG: DataFrame.at must fail when adding more than one value #48362

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

Closed
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
41 changes: 38 additions & 3 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,45 @@ Notable bug fixes

These are bug fixes that might have notable behavior changes.

.. _whatsnew_160.notable_bug_fixes.notable_bug_fix1:
.. _whatsnew_160.notable_bug_fixes.notable_bug_frame_at_new_column_with_slice:

Using :meth:`DataFrame.at` raises when trying to add a new column with multiple values
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*Old behavior*

.. code-block:: ipython

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

In [3]: # should raise InvalidIndexError
df.at[slice(0, 1, None), 'c'] = 7

In [4]: df
Out[4]:
a b c
0 1 4 7.0
1 2 5 7.0
2 3 6 NaN


*New behavior*

.. code-block:: ipython

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

In [3]: df.at[slice(0, 1, None), 'c'] = 7

---------------------------------------------------------------------------
InvalidIndexError Traceback (most recent call last)
<ipython-input-3-a89a95224c2b> in <module>
----> 1 df.at[slice(0, 1, None), 'c'] = 7

notable_bug_fix1
^^^^^^^^^^^^^^^^

.. _whatsnew_160.notable_bug_fixes.notable_bug_fix2:

Expand Down
16 changes: 14 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4206,8 +4206,20 @@ def _set_value(
icol = col
iindex = cast(int, index)
else:
icol = self.columns.get_loc(col)
iindex = self.index.get_loc(index)
exception = None
try:
icol = self.columns.get_loc(col)
except (KeyError, TypeError, ValueError) as e:
exception = e

try:
iindex = self.index.get_loc(index)
except (KeyError, TypeError, ValueError) as e:
exception = e

if exception is not None:
raise exception

self._mgr.column_setitem(icol, iindex, value)
self._clear_item_cache()

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ def test_at_frame_multiple_columns(self):
with pytest.raises(InvalidIndexError, match=r"slice\(None, None, None\)"):
df.at[5] = [6, 7]

def test_at_frame_new_column_when_index_is_slice(self):
# GH-48224
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

with pytest.raises(InvalidIndexError, match=r"slice\(0, 1, None\)"):
df.at[slice(0, 1, None), "c"] = 7

def test_at_getitem_mixed_index_no_fallback(self):
# GH#19860
ser = Series([1, 2, 3, 4, 5], index=["a", "b", "c", 1, 2])
Expand Down