|
9 | 9 | from pandas import (date_range, bdate_range, Timestamp,
|
10 | 10 | Index, MultiIndex, DataFrame, Series,
|
11 | 11 | concat, Panel, DatetimeIndex, read_csv)
|
| 12 | +from pandas.core.dtypes.missing import isna |
12 | 13 | from pandas.errors import UnsupportedFunctionCall, PerformanceWarning
|
13 | 14 | from pandas.util.testing import (assert_frame_equal, assert_index_equal,
|
14 | 15 | assert_series_equal, assert_almost_equal)
|
@@ -2116,6 +2117,27 @@ def interweave(list_obj):
|
2116 | 2117 | exp = DataFrame({'key': keys, 'val': _exp_vals})
|
2117 | 2118 | assert_frame_equal(result, exp)
|
2118 | 2119 |
|
| 2120 | + @pytest.mark.parametrize("skipna", [True, False]) |
| 2121 | + @pytest.mark.parametrize("vals,exp", [ |
| 2122 | + (['foo', 'bar', 'baz'], True), (['foo', '', ''], True), |
| 2123 | + (['', '', ''], False), ([1, 2, 3], True), ([1, 0, 0], True), |
| 2124 | + ([0, 0, 0], False), ([1., 2., 3.], True), ([1., 0., 0.], True), |
| 2125 | + ([0., 0., 0.], False), ([True, True, True], True), |
| 2126 | + ([True, False, False], True), ([False, False, False], False), |
| 2127 | + ([np.nan, np.nan, np.nan], False) |
| 2128 | + ]) |
| 2129 | + def test_groupby_any(self, skipna, vals, exp): |
| 2130 | + df = DataFrame({'key': ['a'] * 3 + ['b'] * 3, 'val': vals * 2}) |
| 2131 | + |
| 2132 | + # edge case for missing data with skipna=False |
| 2133 | + if not(skipna) and all(isna(vals)): |
| 2134 | + exp = True |
| 2135 | + |
| 2136 | + exp_df = DataFrame([exp] * 2, columns=['val'], index=pd.Index( |
| 2137 | + ['a', 'b'], name='key')) |
| 2138 | + result = df.groupby('key').any(skipna=skipna) |
| 2139 | + assert_frame_equal(result, exp_df) |
| 2140 | + |
2119 | 2141 | def test_dont_clobber_name_column(self):
|
2120 | 2142 | df = DataFrame({'key': ['a', 'a', 'a', 'b', 'b', 'b'],
|
2121 | 2143 | 'name': ['foo', 'bar', 'baz'] * 2})
|
|
0 commit comments