|
15 | 15 | import pandas.util.testing as tm
|
16 | 16 |
|
17 | 17 |
|
18 |
| -@pytest.fixture |
19 |
| -def ts(): |
20 |
| - return tm.makeTimeSeries() |
21 |
| - |
22 |
| - |
23 |
| -@pytest.fixture |
24 |
| -def tsframe(): |
25 |
| - return DataFrame(tm.getTimeSeriesData()) |
26 |
| - |
27 |
| - |
28 |
| -@pytest.fixture |
29 |
| -def df(): |
30 |
| - return DataFrame( |
31 |
| - {'A': ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'], |
32 |
| - 'B': ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'], |
33 |
| - 'C': np.random.randn(8), |
34 |
| - 'D': np.random.randn(8)}) |
35 |
| - |
36 |
| - |
37 |
| -@pytest.fixture |
38 |
| -def mframe(): |
39 |
| - index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'], |
40 |
| - ['one', 'two', 'three']], |
41 |
| - labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3], |
42 |
| - [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]], |
43 |
| - names=['first', 'second']) |
44 |
| - return DataFrame(np.random.randn(10, 3), |
45 |
| - index=index, |
46 |
| - columns=['A', 'B', 'C']) |
47 |
| - |
48 |
| - |
49 |
| -@pytest.fixture |
50 |
| -def three_group(): |
51 |
| - return DataFrame( |
52 |
| - {'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', |
53 |
| - 'bar', 'bar', 'foo', 'foo', 'foo'], |
54 |
| - 'B': ['one', 'one', 'one', 'two', 'one', 'one', |
55 |
| - 'one', 'two', 'two', 'two', 'one'], |
56 |
| - 'C': ['dull', 'dull', 'shiny', 'dull', 'dull', 'shiny', |
57 |
| - 'shiny', 'dull', 'shiny', 'shiny', 'shiny'], |
58 |
| - 'D': np.random.randn(11), |
59 |
| - 'E': np.random.randn(11), |
60 |
| - 'F': np.random.randn(11)}) |
61 |
| - |
62 |
| - |
63 | 18 | def test_agg_regression1(tsframe):
|
64 | 19 | grouped = tsframe.groupby([lambda x: x.year, lambda x: x.month])
|
65 | 20 | result = grouped.agg(np.mean)
|
@@ -87,6 +42,32 @@ def test_agg_ser_multi_key(df):
|
87 | 42 | tm.assert_series_equal(results, expected)
|
88 | 43 |
|
89 | 44 |
|
| 45 | +def test_groupby_aggregation_mixed_dtype(): |
| 46 | + |
| 47 | + # GH 6212 |
| 48 | + expected = DataFrame({ |
| 49 | + 'v1': [5, 5, 7, np.nan, 3, 3, 4, 1], |
| 50 | + 'v2': [55, 55, 77, np.nan, 33, 33, 44, 11]}, |
| 51 | + index=MultiIndex.from_tuples([(1, 95), (1, 99), (2, 95), (2, 99), |
| 52 | + ('big', 'damp'), |
| 53 | + ('blue', 'dry'), |
| 54 | + ('red', 'red'), ('red', 'wet')], |
| 55 | + names=['by1', 'by2'])) |
| 56 | + |
| 57 | + df = DataFrame({ |
| 58 | + 'v1': [1, 3, 5, 7, 8, 3, 5, np.nan, 4, 5, 7, 9], |
| 59 | + 'v2': [11, 33, 55, 77, 88, 33, 55, np.nan, 44, 55, 77, 99], |
| 60 | + 'by1': ["red", "blue", 1, 2, np.nan, "big", 1, 2, "red", 1, np.nan, |
| 61 | + 12], |
| 62 | + 'by2': ["wet", "dry", 99, 95, np.nan, "damp", 95, 99, "red", 99, |
| 63 | + np.nan, np.nan] |
| 64 | + }) |
| 65 | + |
| 66 | + g = df.groupby(['by1', 'by2']) |
| 67 | + result = g[['v1', 'v2']].mean() |
| 68 | + tm.assert_frame_equal(result, expected) |
| 69 | + |
| 70 | + |
90 | 71 | def test_agg_apply_corner(ts, tsframe):
|
91 | 72 | # nothing to group, all NA
|
92 | 73 | grouped = ts.groupby(ts * np.nan)
|
|
0 commit comments