-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Deprecate level keyword for dataframe and series aggregations #40869
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
640f15d
Deprecate level keyword for dataframe and series aggregations
phofl 1b9e046
Move raise warning line
phofl e3b1abf
Add warnings to docs
phofl 9f64d6f
Move ok warning
phofl dbe0dfd
Adress review
phofl f3a8083
Merge branch 'master' of https://github.com/pandas-dev/pandas into 39983
phofl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
pandas/tests/frame/methods/test_count_with_level_deprecated.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Index, | ||
Series, | ||
) | ||
import pandas._testing as tm | ||
|
||
|
||
class TestDataFrameCount: | ||
def test_count_multiindex(self, multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
|
||
frame = frame.copy() | ||
frame.index.names = ["a", "b"] | ||
|
||
with tm.assert_produces_warning(FutureWarning): | ||
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) | ||
|
||
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): | ||
with tm.assert_produces_warning(FutureWarning): | ||
frame.count(level="x") | ||
|
||
def test_count_level_corner(self, multiindex_dataframe_random_data): | ||
frame = multiindex_dataframe_random_data | ||
|
||
ser = frame["A"][: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] | ||
with tm.assert_produces_warning(FutureWarning): | ||
result = df.count(level=0) | ||
expected = ( | ||
DataFrame( | ||
index=ser.index.levels[0].set_names(["first"]), columns=df.columns | ||
) | ||
.fillna(0) | ||
.astype(np.int64) | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_count_index_with_nan(self): | ||
# https://github.com/pandas-dev/pandas/issues/21824 | ||
df = DataFrame( | ||
{ | ||
"Person": ["John", "Myla", None, "John", "Myla"], | ||
"Age": [24.0, 5, 21.0, 33, 26], | ||
"Single": [False, True, True, True, False], | ||
} | ||
) | ||
|
||
# count on row labels | ||
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"]), | ||
data=[2, 2], | ||
) | ||
tm.assert_frame_equal(res, expected) | ||
|
||
# count on column labels | ||
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"]), | ||
data=[[2, 2]], | ||
) | ||
tm.assert_frame_equal(res, expected) | ||
|
||
def test_count_level( | ||
self, | ||
multiindex_year_month_day_dataframe_random_data, | ||
multiindex_dataframe_random_data, | ||
): | ||
ymd = multiindex_year_month_day_dataframe_random_data | ||
frame = multiindex_dataframe_random_data | ||
|
||
def _check_counts(frame, axis=0): | ||
index = frame._get_axis(axis) | ||
for i in range(index.nlevels): | ||
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) | ||
|
||
frame.iloc[1, [1, 2]] = np.nan | ||
frame.iloc[7, [0, 1]] = np.nan | ||
ymd.iloc[1, [1, 2]] = np.nan | ||
ymd.iloc[7, [0, 1]] = np.nan | ||
|
||
_check_counts(frame) | ||
_check_counts(ymd) | ||
_check_counts(frame.T, axis=1) | ||
_check_counts(ymd.T, axis=1) | ||
|
||
# can't call with level on regular DataFrame | ||
df = tm.makeTimeDataFrame() | ||
with pytest.raises(TypeError, match="hierarchical"): | ||
with tm.assert_produces_warning(FutureWarning): | ||
df.count(level=0) | ||
|
||
frame["D"] = "foo" | ||
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")) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.