Skip to content

BUG: Changed .at to not set values that do not exist yet in a DataFrame #48323 #48542

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
wants to merge 1 commit into from
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
10 changes: 3 additions & 7 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -851,16 +851,12 @@ You can also set using these same indexers.

.. ipython:: python

df.at[dates[5], 'E'] = 7
df.at[dates[5], 'D'] = 7
df.iat[3, 0] = 7

``at`` may enlarge the object in-place as above if the indexer is missing.

.. ipython:: python

df.at[dates[-1] + pd.Timedelta('1 day'), 0] = 7
df

``at`` will not enlarge the object in-place if the indexer is missing.

Boolean indexing
----------------

Expand Down
37 changes: 34 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,41 @@ 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.at_DataFrame_expand:

notable_bug_fix1
^^^^^^^^^^^^^^^^
Using DataFrame.at to expand DataFrame
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:meth:`DataFrame.at` would allow the addition of columns and rows to a DataFrame. (:issue:`48323`)

.. code-block:: ipython

In [3]: frame = pd.DataFrame({"a": [1, 2]})
In [4]: frame
Out[4]:
a
0 1
1 2

*Old Behavior*

.. code-block:: ipython

In [5]: frame.at[2, "a"] = 7
In [6]: frame
Out[6]:
a
0 1
1 2
2 7

*New Behavior*

.. code-block:: ipython

In [5]: frame.at[2, "a"] = 7
Out[5]:
KeyError: 2

.. _whatsnew_160.notable_bug_fixes.notable_bug_fix2:

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,15 @@ def at(self) -> _AtIndexer:
Raises
------
KeyError
* If getting a value and 'label' does not exist in a DataFrame or
Series.
* If getting or setting a value and 'label' does not exist in a
DataFrame or Series.
ValueError
* If row/column label pair is not a tuple or if any label from
the pair is not a scalar for DataFrame.
* If label is list-like (*excluding* NamedTuple) for Series.

See Also
--------
DataFrame.at : Access a single value for a row/column pair by label.
DataFrame.iat : Access a single value for a row/column pair by integer
position.
DataFrame.loc : Access a group of rows and columns by label(s).
Expand Down Expand Up @@ -2429,6 +2428,9 @@ def __getitem__(self, key):
return super().__getitem__(key)

def __setitem__(self, key, value):
# raises exception if key does not exist
self.__getitem__(key)

if self.ndim == 2 and not self._axes_are_unique:
# GH#33041 fall back to .loc
if not isinstance(key, tuple) or not all(is_scalar(x) for x in key):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def test_set_value_keeps_names():
df = df.sort_index()
assert df._is_copy is None
assert df.index.names == ("Name", "Number")
df.at[("grethe", "4"), "one"] = 99.34
df.loc[("grethe", "4"), "one"] = 99.34
assert df._is_copy is None
assert df.index.names == ("Name", "Number")

Expand Down
23 changes: 16 additions & 7 deletions pandas/tests/indexing/test_at.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import numpy as np
import pytest

from pandas.errors import InvalidIndexError

from pandas import (
CategoricalDtype,
CategoricalIndex,
Expand Down Expand Up @@ -105,12 +103,15 @@ def test_at_setitem_multiindex(self):
np.zeros((3, 2), dtype="int64"),
columns=MultiIndex.from_tuples([("a", 0), ("a", 1)]),
)
df.at[0, "a"] = 10
df.at[0, ("a", 0)] = 10
df.at[0, ("a", 1)] = 10
expected = DataFrame(
[[10, 10], [0, 0], [0, 0]],
columns=MultiIndex.from_tuples([("a", 0), ("a", 1)]),
)
tm.assert_frame_equal(df, expected)
with pytest.raises(TypeError, match=""):
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I would put this into a separate test. One test for the happy path and one for the failing path. YMMV

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to split this because this isn't actually a test I created, but one I had to modify based on the code change, so I'd like to keep the change as small as I reasonably can.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, fair enough.

Personally I am really reluctant to modify existing tests. So I would rather add a new one. YMMV

df.at[0, "a"] = 11

@pytest.mark.parametrize("row", (Timestamp("2019-01-01"), "2019-01-01"))
def test_at_datetime_index(self, row):
Expand All @@ -126,11 +127,13 @@ def test_at_datetime_index(self, row):
tm.assert_frame_equal(df, expected)


class TestAtSetItemWithExpansion:
def test_at_setitem_expansion_series_dt64tz_value(self, tz_naive_fixture):
class TestAtSetTzItem:
def test_at_setitem_series_dt64tz_value(self, tz_naive_fixture):
# GH#25506
# Modified in GH#48323 due to .at change
ts = Timestamp("2017-08-05 00:00:00+0100", tz=tz_naive_fixture)
result = Series(ts)
ts2 = Timestamp("2017-09-05 00:00:00+0100", tz=tz_naive_fixture)
result = Series([ts, ts2])
result.at[1] = ts
expected = Series([ts, ts])
tm.assert_series_equal(result, expected)
Expand Down Expand Up @@ -211,7 +214,7 @@ def test_at_frame_raises_key_error2(self, indexer_al):
def test_at_frame_multiple_columns(self):
# GH#48296 - at shouldn't modify multiple columns
df = DataFrame({"a": [1, 2], "b": [3, 4]})
with pytest.raises(InvalidIndexError, match=r"slice\(None, None, None\)"):
with pytest.raises(TypeError, match="col"):
df.at[5] = [6, 7]

def test_at_getitem_mixed_index_no_fallback(self):
Expand All @@ -234,3 +237,9 @@ def test_at_categorical_integers(self):
for key in [0, 1]:
with pytest.raises(KeyError, match=str(key)):
df.at[key, key]

def test_at_does_not_expand(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should have two tests here, one for expansion in column axis and another one for expansion in index axis.

# GH#48323
frame = DataFrame({"a": [1, 2]})
with pytest.raises(KeyError, match="b"):
frame.at[2, "b"] = 9
6 changes: 0 additions & 6 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,13 @@ def test_partial_setting2(self):
df = df_orig.copy()
df.loc[dates[-1] + dates.freq, "A"] = 7
tm.assert_frame_equal(df, expected)
df = df_orig.copy()
df.at[dates[-1] + dates.freq, "A"] = 7
tm.assert_frame_equal(df, expected)

exp_other = DataFrame({0: 7}, index=dates[-1:] + dates.freq)
expected = pd.concat([df_orig, exp_other], axis=1)

df = df_orig.copy()
df.loc[dates[-1] + dates.freq, 0] = 7
tm.assert_frame_equal(df, expected)
df = df_orig.copy()
df.at[dates[-1] + dates.freq, 0] = 7
tm.assert_frame_equal(df, expected)

def test_partial_setting_mixed_dtype(self):

Expand Down