Skip to content

TST: Removed import of itertools #32364

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
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
33 changes: 15 additions & 18 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import builtins
import datetime as dt
from io import StringIO
from itertools import product
from string import ascii_lowercase

import numpy as np
Expand Down Expand Up @@ -1296,36 +1295,32 @@ def __eq__(self, other):
# --------------------------------


def test_size(df):
grouped = df.groupby(["A", "B"])
@pytest.mark.parametrize("by", ["A", "B", ["A", "B"]])
def test_size(df, by):
grouped = df.groupby(by=by)
result = grouped.size()
for key, group in grouped:
assert result[key] == len(group)

grouped = df.groupby("A")
result = grouped.size()
for key, group in grouped:
assert result[key] == len(group)

grouped = df.groupby("B")
result = grouped.size()
for key, group in grouped:
assert result[key] == len(group)
@pytest.mark.parametrize("by", ["A", "B", ["A", "B"]])
@pytest.mark.parametrize("sort", [True, False])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This brings the total number of sort parameterizations for [True, False] to 12. so maybe now worth considering creating a fixture for this. (as a follow-up)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is already a fixture for sort but it's only for [None, False] IIRC.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use different fixtures with the same name. The fixture used depends on the directory structure.

we have the relevant fixture in pandas\tests\reshape\test_concat.py, so just needs to be moved.

The sort fixture in pandas\tests\indexes\conftest.py has a caution note. maybe worth adding something like that.

BUT, if you do this, do in a follow-up, not here.

def test_size_sort(df, sort, by):
df = DataFrame(np.random.choice(20, (1000, 3)), columns=list("ABC"))
left = df.groupby(by=by, sort=sort).size()
right = df.groupby(by=by, sort=sort)["C"].apply(lambda a: a.shape[0])
tm.assert_series_equal(left, right, check_names=False)

df = DataFrame(np.random.choice(20, (1000, 3)), columns=list("abc"))
for sort, key in product((False, True), ("a", "b", ["a", "b"])):
left = df.groupby(key, sort=sort).size()
right = df.groupby(key, sort=sort)["c"].apply(lambda a: a.shape[0])
tm.assert_series_equal(left, right, check_names=False)

# GH11699
def test_size_series_dataframe():
# https://github.com/pandas-dev/pandas/issues/11699
df = DataFrame(columns=["A", "B"])
out = Series(dtype="int64", index=Index([], name="A"))
tm.assert_series_equal(df.groupby("A").size(), out)


def test_size_groupby_all_null():
# GH23050
# https://github.com/pandas-dev/pandas/issues/23050
# Assert no 'Value Error : Length of passed values is 2, index implies 0'
df = DataFrame({"A": [None, None]}) # all-null groups
result = df.groupby("A").size()
Expand All @@ -1335,6 +1330,8 @@ def test_size_groupby_all_null():

# quantile
# --------------------------------


@pytest.mark.parametrize(
"interpolation", ["linear", "lower", "higher", "nearest", "midpoint"]
)
Expand Down