1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
| import numpy as np
|
| import pandas as pd
| import pandas._testing as tm
|
|
| def test_group_by_copy():
| # GH#44803
| df = pd.DataFrame(
| {
| "name": ["Alice", "Bob", "Carl"],
| "age": [20, 21, 20],
| }
| ).set_index("name")
|
| grp_by_same_value = df.groupby(["age"], group_keys=False).apply(lambda group: group)
| grp_by_copy = df.groupby(["age"], group_keys=False).apply(
| lambda group: group.copy()
| )
| tm.assert_frame_equal(grp_by_same_value, grp_by_copy)
|
|
| def test_mutate_groups():
| # GH3380
|
| df = pd.DataFrame(
| {
| "cat1": ["a"] * 8 + ["b"] * 6,
| "cat2": ["c"] * 2
| + ["d"] * 2
| + ["e"] * 2
| + ["f"] * 2
| + ["c"] * 2
| + ["d"] * 2
| + ["e"] * 2,
| "cat3": [f"g{x}" for x in range(1, 15)],
| "val": np.random.randint(100, size=14),
| }
| )
|
| def f_copy(x):
| x = x.copy()
| x["rank"] = x.val.rank(method="min")
| return x.groupby("cat2")["rank"].min()
|
| def f_no_copy(x):
| x["rank"] = x.val.rank(method="min")
| return x.groupby("cat2")["rank"].min()
|
| grpby_copy = df.groupby("cat1").apply(f_copy)
| grpby_no_copy = df.groupby("cat1").apply(f_no_copy)
| tm.assert_series_equal(grpby_copy, grpby_no_copy)
|
|
| def test_no_mutate_but_looks_like():
| # GH 8467
| # first show's mutation indicator
| # second does not, but should yield the same results
| df = pd.DataFrame({"key": [1, 1, 1, 2, 2, 2, 3, 3, 3], "value": range(9)})
|
| result1 = df.groupby("key", group_keys=True).apply(lambda x: x[:].key)
| result2 = df.groupby("key", group_keys=True).apply(lambda x: x.key)
| tm.assert_series_equal(result1, result2)
|
|
| def test_apply_function_with_indexing():
| # GH: 33058
| df = pd.DataFrame(
| {"col1": ["A", "A", "A", "B", "B", "B"], "col2": [1, 2, 3, 4, 5, 6]}
| )
|
| def fn(x):
| x.loc[x.index[-1], "col2"] = 0
| return x.col2
|
| result = df.groupby(["col1"], as_index=False).apply(fn)
| expected = pd.Series(
| [1, 2, 0, 4, 5, 0],
| index=pd.MultiIndex.from_tuples(
| [(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
| ),
| name="col2",
| )
| tm.assert_series_equal(result, expected)
|
|
| def test_apply_mutate_columns_multiindex():
| # GH 12652
| df = pd.DataFrame(
| {
| ("C", "julian"): [1, 2, 3],
| ("B", "geoffrey"): [1, 2, 3],
| ("A", "julian"): [1, 2, 3],
| ("B", "julian"): [1, 2, 3],
| ("A", "geoffrey"): [1, 2, 3],
| ("C", "geoffrey"): [1, 2, 3],
| },
| columns=pd.MultiIndex.from_tuples(
| [
| ("A", "julian"),
| ("A", "geoffrey"),
| ("B", "julian"),
| ("B", "geoffrey"),
| ("C", "julian"),
| ("C", "geoffrey"),
| ]
| ),
| )
|
| def add_column(grouped):
| name = grouped.columns[0][1]
| grouped["sum", name] = grouped.sum(axis=1)
| return grouped
|
| result = df.groupby(level=1, axis=1).apply(add_column)
| expected = pd.DataFrame(
| [
| [1, 1, 1, 3, 1, 1, 1, 3],
| [2, 2, 2, 6, 2, 2, 2, 6],
| [
| 3,
| 3,
| 3,
| 9,
| 3,
| 3,
| 3,
| 9,
| ],
| ],
| columns=pd.MultiIndex.from_tuples(
| [
| ("geoffrey", "A", "geoffrey"),
| ("geoffrey", "B", "geoffrey"),
| ("geoffrey", "C", "geoffrey"),
| ("geoffrey", "sum", "geoffrey"),
| ("julian", "A", "julian"),
| ("julian", "B", "julian"),
| ("julian", "C", "julian"),
| ("julian", "sum", "julian"),
| ]
| ),
| )
| tm.assert_frame_equal(result, expected)
|
|