-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Multiindexed series .at fix #32520
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
083b1ec
88bca04
f325e09
d03b102
bf4f1c4
89ee128
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 |
---|---|---|
|
@@ -351,3 +351,65 @@ def test_iat_series_with_period_index(): | |
expected = ser[index[0]] | ||
result = ser.iat[0] | ||
assert expected == result | ||
|
||
|
||
def test_at_with_tuple_index_get(): | ||
# GH 26989 | ||
# DataFrame.at getter works with Index of tuples | ||
df = DataFrame({"a": [1, 2]}, index=[(1, 2), (3, 4)]) | ||
assert df.index.nlevels == 1 | ||
assert df.at[(1, 2), "a"] == 1 | ||
|
||
# Series.at getter works with Index of tuples | ||
series = df["a"] | ||
assert series.index.nlevels == 1 | ||
assert series.at[(1, 2)] == 1 | ||
|
||
|
||
def test_at_with_tuple_index_set(): | ||
# GH 26989 | ||
# DataFrame.at setter works with Index of tuples | ||
df = DataFrame({"a": [1, 2]}, index=[(1, 2), (3, 4)]) | ||
assert df.index.nlevels == 1 | ||
df.at[(1, 2), "a"] = 2 | ||
assert df.at[(1, 2), "a"] == 2 | ||
|
||
# Series.at setter works with Index of tuples | ||
series = df["a"] | ||
assert series.index.nlevels == 1 | ||
series.at[1, 2] = 3 | ||
assert series.at[1, 2] == 3 | ||
|
||
|
||
def test_multiindex_at_get(): | ||
# GH 26989 | ||
# DataFrame.at and DataFrame.loc getter works with MultiIndex | ||
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]]) | ||
assert df.index.nlevels == 2 | ||
assert df.at[(1, 3), "a"] == 1 | ||
assert df.loc[(1, 3), "a"] == 1 | ||
|
||
# Series.at and Series.loc getter works with MultiIndex | ||
series = df["a"] | ||
assert series.index.nlevels == 2 | ||
assert series.at[1, 3] == 1 | ||
assert series.loc[1, 3] == 1 | ||
|
||
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. Can you add a test for not providing all MultiIndex levels? (so eg 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. And maybe also an equivalent test for a DataFrame (it seems to work, but would be good to ensure we also have an explicit test for that) 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. Sure - I have it mostly done, but ran into a question. For the series
i.e.
what should be the output of series.at[1, 3]? I was expecting to just get the scalar 1, but currently I'm getting
After thinking about this - perhaps what we have currently is the desired output. My reason for this is that the shape doesn't change depending on the input - (1, 3) vs (2, 4). 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. IMO, that should be a scalar (but that is also the reason that I asked to test it, to be sure this is covered ànd to discuss the semantics we want) |
||
|
||
def test_multiindex_at_set(): | ||
# GH 26989 | ||
# DataFrame.at and DataFrame.loc setter works with MultiIndex | ||
df = DataFrame({"a": [1, 2]}, index=[[1, 2], [3, 4]]) | ||
assert df.index.nlevels == 2 | ||
df.at[(1, 3), "a"] = 3 | ||
assert df.at[(1, 3), "a"] == 3 | ||
df.loc[(1, 3), "a"] = 4 | ||
assert df.loc[(1, 3), "a"] == 4 | ||
|
||
# Series.at and Series.loc setter works with MultiIndex | ||
series = df["a"] | ||
assert series.index.nlevels == 2 | ||
series.at[1, 3] = 5 | ||
assert series.at[1, 3] == 5 | ||
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. is this the behavior now on master? IOW is this an api change 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. Current behavior on master is to raise ValueError for any of the at lines, behavior with the loc lines is the same as master. I would describe this as a bugfix and not an api change. |
||
series.loc[1, 3] = 6 | ||
assert series.loc[1, 3] == 6 |
Uh oh!
There was an error while loading. Please reload this page.