-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,6 @@ | |
import numpy as np | ||
import pytest | ||
|
||
from pandas.errors import InvalidIndexError | ||
|
||
from pandas import ( | ||
CategoricalDtype, | ||
CategoricalIndex, | ||
|
@@ -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=""): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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) | ||
|
@@ -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): | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Uh oh!
There was an error while loading. Please reload this page.