Skip to content
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 6 commits into from
Aug 21, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,193 @@ def test_groupby_get_by_index():
pd.testing.assert_frame_equal(res, expected)


def test_groupby_single_agg_numeric_col():
"""
Copy link
Contributor

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

test single agg on a numeric column
"""

# 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"})

# create expected dataframe
cat_index = pd.CategoricalIndex(
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category"
)

expected_df = pd.DataFrame(data={"nr": [1, 5]}, index=cat_index)

tm.assert_frame_equal(result_df, expected_df)


def test_groupby_single_agg_cat_cols():
"""
test single agg on a ordered categorical column
"""

# 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({"cat_ord": "min"})

# create expected dataframe
cat_index = pd.CategoricalIndex(
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category"
)

expected_df = pd.DataFrame(data={"cat_ord": ["a", "c"]}, index=cat_index)

tm.assert_frame_equal(result_df, expected_df)


def test_groupby_combined_single_agg_cat_cols():
"""
test combined single aggregations on a
numeric and multiple aggregation an ordered
categorical column
"""

# 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", "cat_ord": "min"})

# create expected dataframe
cat_index = pd.CategoricalIndex(
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category"
)

expected_df = pd.DataFrame(
data={"nr": [1, 5], "cat_ord": ["a", "c"]}, index=cat_index
)

tm.assert_frame_equal(result_df, expected_df)


def test_groupby_multiple_agg_cat_cols():
"""
test multiple aggregations on an ordered categorical column
"""

# 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({"cat_ord": ["min", "max"]})

# create expected dataframe
cat_index = pd.CategoricalIndex(
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category"
)

multi_index_tuple = [("cat_ord", "min"), ("cat_ord", "max")]
multi_index = pd.MultiIndex.from_tuples(multi_index_tuple)

data = [("a", "b"), ("c", "d")]
expected_df = pd.DataFrame(data=data, columns=multi_index, index=cat_index)

tm.assert_frame_equal(result_df, expected_df)


def test_groupby_combined_multiple_agg_cat_cols():
"""
test single aggregations on a numeric and
multiple aggregations an ordered categorical column
"""

# 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", "cat_ord": ["min", "max"]})

# create expected dataframe
cat_index = pd.CategoricalIndex(
["a", "b"], categories=["a", "b"], ordered=False, name="cat", dtype="category"
)

multi_index_tuple = [("nr", "min"), ("cat_ord", "min"), ("cat_ord", "max")]
multi_index = pd.MultiIndex.from_tuples(multi_index_tuple)

data = [(1, "a", "b"), (5, "c", "d")]
expected_df = pd.DataFrame(data=data, columns=multi_index, index=cat_index)

tm.assert_frame_equal(result_df, expected_df)


def test_groupby_combined_multiple_numeric_cat_cols():
"""
test multiple aggregation on numeric and a
single aggregation on an ordered categorical
column #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 = [(1, 4, "a"), (5, 8, "c")]
expected_df = pd.DataFrame(data=data, columns=multi_index, index=cat_index)

tm.assert_frame_equal(result_df, expected_df)


def test_nonagg_agg():
# GH 35490 - Single/Multiple agg of non-agg function give same results
# TODO: agg should raise for functions that don't aggregate
Expand Down