-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
TST: add test for agg on ordered categorical cols #35630
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2320e9
add test for agg on ordered categorical cols
87c9eae
replace np arr and dtype conversion with list
ae5c1e3
add more test cases for ordered categorical columns
2759f3d
resolve merge conflicts
3380aa4
empty commit to trigger travis rebuild
603602d
parameterize tests for ordered cat col aggregations
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 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 |
---|---|---|
|
@@ -1061,3 +1061,37 @@ def test_groupby_get_by_index(): | |
res = df.groupby("A").agg({"B": lambda x: x.get(x.index[-1])}) | ||
expected = pd.DataFrame(dict(A=["S", "W"], B=[1.0, 2.0])).set_index("A") | ||
pd.testing.assert_frame_equal(res, expected) | ||
|
||
|
||
def test_groupby_agg_categorical_cols(): | ||
""" | ||
test aggregation on ordered categorical | ||
columns #27800 | ||
""" | ||
|
||
# create the result dataframe | ||
input_df = pd.DataFrame( | ||
{ | ||
"nr": [1, 2, 3, 4, 5, 6, 7, 8], | ||
"cat_ord": list("aabbccdd"), | ||
"cat": list("aaaabbbb"), | ||
} | ||
) | ||
|
||
input_df = input_df.astype({"cat": "category", "cat_ord": "category"}) | ||
input_df["cat_ord"] = input_df["cat_ord"].cat.as_ordered() | ||
result_df = input_df.groupby("cat").agg({"nr": ["min", "max"], "cat_ord": "min"}) | ||
|
||
# create expected dataframe | ||
cat_index = pd.CategoricalIndex( | ||
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category" | ||
) | ||
|
||
multi_index_tuple = [("nr", "min"), ("nr", "max"), ("cat_ord", "min")] | ||
multi_index = pd.MultiIndex.from_tuples(multi_index_tuple) | ||
|
||
data = np.array([(1, 4, "a"), (5, 8, "c")]) | ||
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 think if you just pass this in as a list, then you won't have to |
||
expected_df = pd.DataFrame(data=data, columns=multi_index, index=cat_index) | ||
expected_df["nr"] = expected_df["nr"].astype("int64") | ||
|
||
tm.assert_frame_equal(result_df, expected_df) |
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.
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.
pls add the issue number as a comment.
you don't really need the doc-string; the test name should be descriptive enough