-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
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
Deprecate level keyword for dataframe and series aggregations #40869
Changes from 4 commits
640f15d
1b9e046
e3b1abf
9f64d6f
dbe0dfd
f3a8083
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 |
---|---|---|
|
@@ -625,6 +625,7 @@ even if some categories are not present in the data: | |
``DataFrame`` methods like :meth:`DataFrame.sum` also show "unused" categories. | ||
|
||
.. ipython:: python | ||
:okwarning: | ||
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 change this so its a regular group op 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. Done |
||
|
||
columns = pd.Categorical( | ||
["One", "One", "Two"], categories=["One", "Two", "Three"], ordered=True | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,7 @@ directly. Additionally, the resulting index will be named according to the | |
chosen level: | ||
|
||
.. ipython:: python | ||
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. point the example from advanced.rst here (and make this a proper groupby example) 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. Removed, since above is the groupby case |
||
:okwarning: | ||
|
||
s.sum(level="second") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,22 @@ def test_count_multiindex(self, multiindex_dataframe_random_data): | |
frame = frame.copy() | ||
frame.index.names = ["a", "b"] | ||
|
||
result = frame.count(level="b") | ||
expected = frame.count(level=1) | ||
with tm.assert_produces_warning(FutureWarning): | ||
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 move these to a new test_count_with_level_deprecated 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. yep, done |
||
result = frame.count(level="b") | ||
with tm.assert_produces_warning(FutureWarning): | ||
expected = frame.count(level=1) | ||
tm.assert_frame_equal(result, expected, check_names=False) | ||
|
||
result = frame.count(level="a") | ||
expected = frame.count(level=0) | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = frame.count(level="a") | ||
with tm.assert_produces_warning(FutureWarning): | ||
expected = frame.count(level=0) | ||
tm.assert_frame_equal(result, expected, check_names=False) | ||
|
||
msg = "Level x not found" | ||
with pytest.raises(KeyError, match=msg): | ||
frame.count(level="x") | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.count(level="x") | ||
|
||
def test_count(self): | ||
# corner case | ||
|
@@ -64,12 +69,14 @@ def test_count_level_corner(self, multiindex_dataframe_random_data): | |
frame = multiindex_dataframe_random_data | ||
|
||
ser = frame["A"][:0] | ||
result = ser.count(level=0) | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = ser.count(level=0) | ||
expected = Series(0, index=ser.index.levels[0], name="A") | ||
tm.assert_series_equal(result, expected) | ||
|
||
df = frame[:0] | ||
result = df.count(level=0) | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = df.count(level=0) | ||
expected = ( | ||
DataFrame( | ||
index=ser.index.levels[0].set_names(["first"]), columns=df.columns | ||
|
@@ -90,7 +97,8 @@ def test_count_index_with_nan(self): | |
) | ||
|
||
# count on row labels | ||
res = df.set_index(["Person", "Single"]).count(level="Person") | ||
with tm.assert_produces_warning(FutureWarning): | ||
res = df.set_index(["Person", "Single"]).count(level="Person") | ||
expected = DataFrame( | ||
index=Index(["John", "Myla"], name="Person"), | ||
columns=Index(["Age"]), | ||
|
@@ -99,7 +107,8 @@ def test_count_index_with_nan(self): | |
tm.assert_frame_equal(res, expected) | ||
|
||
# count on column labels | ||
res = df.set_index(["Person", "Single"]).T.count(level="Person", axis=1) | ||
with tm.assert_produces_warning(FutureWarning): | ||
res = df.set_index(["Person", "Single"]).T.count(level="Person", axis=1) | ||
expected = DataFrame( | ||
columns=Index(["John", "Myla"], name="Person"), | ||
index=Index(["Age"]), | ||
|
@@ -118,7 +127,8 @@ def test_count_level( | |
def _check_counts(frame, axis=0): | ||
index = frame._get_axis(axis) | ||
for i in range(index.nlevels): | ||
result = frame.count(axis=axis, level=i) | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = frame.count(axis=axis, level=i) | ||
expected = frame.groupby(axis=axis, level=i).count() | ||
expected = expected.reindex_like(result).astype("i8") | ||
tm.assert_frame_equal(result, expected) | ||
|
@@ -136,8 +146,10 @@ def _check_counts(frame, axis=0): | |
# can't call with level on regular DataFrame | ||
df = tm.makeTimeDataFrame() | ||
with pytest.raises(TypeError, match="hierarchical"): | ||
df.count(level=0) | ||
with tm.assert_produces_warning(FutureWarning): | ||
df.count(level=0) | ||
|
||
frame["D"] = "foo" | ||
result = frame.count(level=0, numeric_only=True) | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = frame.count(level=0, numeric_only=True) | ||
tm.assert_index_equal(result.columns, Index(list("ABC"), name="exp")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would just remove this example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may instead show a groupby example of this (but then i would put it in the groupby section), you can leave a pointer if you'd like here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If ok, I would keep it there, there are level examples above and below this snippet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But changed to groupby