BUG: do not crash on a callable grouper returning tuples · pandas-dev/pandas@9123f7d (original) (raw)
`@@ -202,6 +202,23 @@ def test_grouper_creation_bug(self):
`
202
202
`expected = s.groupby(level='one').sum()
`
203
203
`assert_series_equal(result, expected)
`
204
204
``
``
205
`+
@pytest.mark.parametrize('func', [False, True])
`
``
206
`+
def test_grouper_returning_tuples(self, func):
`
``
207
`+
GH 22257 , both with dict and with callable
`
``
208
`+
df = pd.DataFrame({'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]})
`
``
209
`+
mapping = dict(zip(range(4), [('C', 5), ('D', 6)] * 2))
`
``
210
+
``
211
`+
if func:
`
``
212
`+
gb = df.groupby(by=lambda idx: mapping[idx], sort=False)
`
``
213
`+
else:
`
``
214
`+
gb = df.groupby(by=mapping, sort=False)
`
``
215
+
``
216
`+
name, expected = list(gb)[0]
`
``
217
`+
assert name == ('C', 5)
`
``
218
`+
result = gb.get_group(name)
`
``
219
+
``
220
`+
assert_frame_equal(result, expected)
`
``
221
+
205
222
`def test_grouper_column_and_index(self):
`
206
223
`# GH 14327
`
207
224
``
`@@ -346,7 +363,7 @@ def test_groupby_grouper_f_sanity_checked(self):
`
346
363
`# when the elements are Timestamp.
`
347
364
`# the result is Index[0:6], very confusing.
`
348
365
``
349
``
`-
pytest.raises(AssertionError, ts.groupby, lambda key: key[0:6])
`
``
366
`+
pytest.raises(ValueError, ts.groupby, lambda key: key[0:6])
`
350
367
``
351
368
`def test_grouping_error_on_multidim_input(self, df):
`
352
369
`pytest.raises(ValueError,
`