Skip to content

Commit 4c359a3

Browse files
authored
TST: Move groupby tests out of test_function (pandas-dev#55967)
* TST: Move groupby tests out of test_function * one more
1 parent 5457e59 commit 4c359a3

File tree

6 files changed

+319
-321
lines changed

6 files changed

+319
-321
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

+6
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ def test_agg_multiple_functions_maintain_order(df):
356356
tm.assert_index_equal(result.columns, exp_cols)
357357

358358

359+
def test_series_index_name(df):
360+
grouped = df.loc[:, ["C"]].groupby(df["A"])
361+
result = grouped.agg(lambda x: x.mean())
362+
assert result.index.name == "A"
363+
364+
359365
def test_agg_multiple_functions_same_name():
360366
# GH 30880
361367
df = DataFrame(

pandas/tests/groupby/methods/test_describe.py

+70
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,73 @@ def test_describe_duplicate_columns():
219219
)
220220
expected.index.names = [1]
221221
tm.assert_frame_equal(result, expected)
222+
223+
224+
class TestGroupByNonCythonPaths:
225+
# GH#5610 non-cython calls should not include the grouper
226+
# Tests for code not expected to go through cython paths.
227+
228+
@pytest.fixture
229+
def df(self):
230+
df = DataFrame(
231+
[[1, 2, "foo"], [1, np.nan, "bar"], [3, np.nan, "baz"]],
232+
columns=["A", "B", "C"],
233+
)
234+
return df
235+
236+
@pytest.fixture
237+
def gb(self, df):
238+
gb = df.groupby("A")
239+
return gb
240+
241+
@pytest.fixture
242+
def gni(self, df):
243+
gni = df.groupby("A", as_index=False)
244+
return gni
245+
246+
def test_describe(self, df, gb, gni):
247+
# describe
248+
expected_index = Index([1, 3], name="A")
249+
expected_col = MultiIndex(
250+
levels=[["B"], ["count", "mean", "std", "min", "25%", "50%", "75%", "max"]],
251+
codes=[[0] * 8, list(range(8))],
252+
)
253+
expected = DataFrame(
254+
[
255+
[1.0, 2.0, np.nan, 2.0, 2.0, 2.0, 2.0, 2.0],
256+
[0.0, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
257+
],
258+
index=expected_index,
259+
columns=expected_col,
260+
)
261+
result = gb.describe()
262+
tm.assert_frame_equal(result, expected)
263+
264+
expected = expected.reset_index()
265+
result = gni.describe()
266+
tm.assert_frame_equal(result, expected)
267+
268+
269+
@pytest.mark.parametrize("dtype", [int, float, object])
270+
@pytest.mark.parametrize(
271+
"kwargs",
272+
[
273+
{"percentiles": [0.10, 0.20, 0.30], "include": "all", "exclude": None},
274+
{"percentiles": [0.10, 0.20, 0.30], "include": None, "exclude": ["int"]},
275+
{"percentiles": [0.10, 0.20, 0.30], "include": ["int"], "exclude": None},
276+
],
277+
)
278+
def test_groupby_empty_dataset(dtype, kwargs):
279+
# GH#41575
280+
df = DataFrame([[1, 2, 3]], columns=["A", "B", "C"], dtype=dtype)
281+
df["B"] = df["B"].astype(int)
282+
df["C"] = df["C"].astype(float)
283+
284+
result = df.iloc[:0].groupby("A").describe(**kwargs)
285+
expected = df.groupby("A").describe(**kwargs).reset_index(drop=True).iloc[:0]
286+
tm.assert_frame_equal(result, expected)
287+
288+
result = df.iloc[:0].groupby("A").B.describe(**kwargs)
289+
expected = df.groupby("A").B.describe(**kwargs).reset_index(drop=True).iloc[:0]
290+
expected.index = Index([])
291+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_apply.py

+42
Original file line numberDiff line numberDiff line change
@@ -1559,3 +1559,45 @@ def test_include_groups(include_groups):
15591559
if not include_groups:
15601560
expected = expected[["b"]]
15611561
tm.assert_frame_equal(result, expected)
1562+
1563+
1564+
@pytest.mark.parametrize("f", [max, min, sum])
1565+
@pytest.mark.parametrize("keys", ["jim", ["jim", "joe"]]) # Single key # Multi-key
1566+
def test_builtins_apply(keys, f):
1567+
# see gh-8155
1568+
rs = np.random.default_rng(2)
1569+
df = DataFrame(rs.integers(1, 7, (10, 2)), columns=["jim", "joe"])
1570+
df["jolie"] = rs.standard_normal(10)
1571+
1572+
gb = df.groupby(keys)
1573+
1574+
fname = f.__name__
1575+
1576+
warn = None if f is not sum else FutureWarning
1577+
msg = "The behavior of DataFrame.sum with axis=None is deprecated"
1578+
with tm.assert_produces_warning(
1579+
warn, match=msg, check_stacklevel=False, raise_on_extra_warnings=False
1580+
):
1581+
# Also warns on deprecation GH#53425
1582+
result = gb.apply(f)
1583+
ngroups = len(df.drop_duplicates(subset=keys))
1584+
1585+
assert_msg = f"invalid frame shape: {result.shape} (expected ({ngroups}, 3))"
1586+
assert result.shape == (ngroups, 3), assert_msg
1587+
1588+
npfunc = lambda x: getattr(np, fname)(x, axis=0) # numpy's equivalent function
1589+
msg = "DataFrameGroupBy.apply operated on the grouping columns"
1590+
with tm.assert_produces_warning(FutureWarning, match=msg):
1591+
expected = gb.apply(npfunc)
1592+
tm.assert_frame_equal(result, expected)
1593+
1594+
with tm.assert_produces_warning(FutureWarning, match=msg):
1595+
expected2 = gb.apply(lambda x: npfunc(x))
1596+
tm.assert_frame_equal(result, expected2)
1597+
1598+
if f != sum:
1599+
expected = gb.agg(fname).reset_index()
1600+
expected.set_index(keys, inplace=True, drop=False)
1601+
tm.assert_frame_equal(result, expected, check_dtype=False)
1602+
1603+
tm.assert_series_equal(getattr(result, fname)(axis=0), getattr(df, fname)(axis=0))

pandas/tests/groupby/test_cumulative.py

+27
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,30 @@ def test_nullable_int_not_cast_as_float(method, dtype, val):
289289
expected = DataFrame({"b": data}, dtype=dtype)
290290

291291
tm.assert_frame_equal(result, expected)
292+
293+
294+
def test_cython_api2():
295+
# this takes the fast apply path
296+
297+
# cumsum (GH5614)
298+
df = DataFrame([[1, 2, np.nan], [1, np.nan, 9], [3, 4, 9]], columns=["A", "B", "C"])
299+
expected = DataFrame([[2, np.nan], [np.nan, 9], [4, 9]], columns=["B", "C"])
300+
result = df.groupby("A").cumsum()
301+
tm.assert_frame_equal(result, expected)
302+
303+
# GH 5755 - cumsum is a transformer and should ignore as_index
304+
result = df.groupby("A", as_index=False).cumsum()
305+
tm.assert_frame_equal(result, expected)
306+
307+
# GH 13994
308+
msg = "DataFrameGroupBy.cumsum with axis=1 is deprecated"
309+
with tm.assert_produces_warning(FutureWarning, match=msg):
310+
result = df.groupby("A").cumsum(axis=1)
311+
expected = df.cumsum(axis=1)
312+
tm.assert_frame_equal(result, expected)
313+
314+
msg = "DataFrameGroupBy.cumprod with axis=1 is deprecated"
315+
with tm.assert_produces_warning(FutureWarning, match=msg):
316+
result = df.groupby("A").cumprod(axis=1)
317+
expected = df.cumprod(axis=1)
318+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)