Skip to content

Commit 3783ccc

Browse files
zym1010jreback
authored andcommitted
add test cases for GroupBy.apply trivial cases (#20081)
1 parent 7230991 commit 3783ccc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pandas/tests/groupby/test_groupby.py

+30
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,36 @@ def test_apply_issues(self):
233233
lambda x: x['time'][x['value'].idxmax()])
234234
assert_series_equal(result, expected)
235235

236+
def test_apply_trivial(self):
237+
# GH 20066
238+
# trivial apply: ignore input and return a constant dataframe.
239+
df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'],
240+
'data': [1.0, 2.0, 3.0, 4.0, 5.0]},
241+
columns=['key', 'data'])
242+
expected = pd.concat([df.iloc[1:], df.iloc[1:]],
243+
axis=1, keys=['float64', 'object'])
244+
result = df.groupby([str(x) for x in df.dtypes],
245+
axis=1).apply(lambda x: df.iloc[1:])
246+
247+
assert_frame_equal(result, expected)
248+
249+
@pytest.mark.xfail(reason=("GH 20066; function passed into apply "
250+
"returns a DataFrame with the same index "
251+
"as the one to create GroupBy object."))
252+
def test_apply_trivial_fail(self):
253+
# GH 20066
254+
# trivial apply fails if the constant dataframe has the same index
255+
# with the one used to create GroupBy object.
256+
df = pd.DataFrame({'key': ['a', 'a', 'b', 'b', 'a'],
257+
'data': [1.0, 2.0, 3.0, 4.0, 5.0]},
258+
columns=['key', 'data'])
259+
expected = pd.concat([df, df],
260+
axis=1, keys=['float64', 'object'])
261+
result = df.groupby([str(x) for x in df.dtypes],
262+
axis=1).apply(lambda x: df)
263+
264+
assert_frame_equal(result, expected)
265+
236266
def test_time_field_bug(self):
237267
# Test a fix for the following error related to GH issue 11324 When
238268
# non-key fields in a group-by dataframe contained time-based fields

0 commit comments

Comments
 (0)