|
| 1 | +""" |
| 2 | +Tests for DataFrame.agg with numeric_only parameter and list of functions. |
| 3 | +This tests the fix for GH#49352. |
| 4 | +""" |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | +import pandas as pd |
| 9 | +from pandas import DataFrame |
| 10 | +import pandas._testing as tm |
| 11 | + |
| 12 | + |
| 13 | +class TestFrameAggNumericOnly: |
| 14 | + """Tests for DataFrame.agg with numeric_only parameter and list of functions.""" |
| 15 | + |
| 16 | + def test_agg_list_numeric_only_mixed_dtypes(self): |
| 17 | + """GH#49352 - Main test case from the issue.""" |
| 18 | + df = DataFrame({ |
| 19 | + 'A': [1, 2, 3, 4, 5], |
| 20 | + 'B': [10.5, 20.5, 30.5, 40.5, 50.5], |
| 21 | + 'C': ['a', 'b', 'c', 'd', 'e'] |
| 22 | + }) |
| 23 | + result = df.agg(['min', 'max', 'mean'], numeric_only=True) |
| 24 | + expected = DataFrame({ |
| 25 | + 'A': [1.0, 5.0, 3.0], |
| 26 | + 'B': [10.5, 50.5, 30.5] |
| 27 | + }, index=['min', 'max', 'mean']) |
| 28 | + tm.assert_frame_equal(result, expected) |
| 29 | + |
| 30 | + def test_agg_list_numeric_only_all_numeric(self): |
| 31 | + """Should work when all columns are numeric.""" |
| 32 | + df = DataFrame({ |
| 33 | + 'A': [1, 2, 3], |
| 34 | + 'B': [10, 20, 30] |
| 35 | + }) |
| 36 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 37 | + expected = DataFrame({ |
| 38 | + 'A': [6.0, 2.0], |
| 39 | + 'B': [60.0, 20.0] |
| 40 | + }, index=['sum', 'mean']) |
| 41 | + tm.assert_frame_equal(result, expected) |
| 42 | + |
| 43 | + def test_agg_list_numeric_only_no_numeric(self): |
| 44 | + """Should return empty DataFrame when no numeric columns.""" |
| 45 | + df = DataFrame({ |
| 46 | + 'A': ['a', 'b', 'c'], |
| 47 | + 'B': ['x', 'y', 'z'] |
| 48 | + }) |
| 49 | + result = df.agg(['min', 'max'], numeric_only=True) |
| 50 | + expected = DataFrame(index=['min', 'max']) |
| 51 | + tm.assert_frame_equal(result, expected) |
| 52 | + |
| 53 | + @pytest.mark.parametrize("funcs,expected_index", [ |
| 54 | + (['sum', 'mean'], ['sum', 'mean']), |
| 55 | + ([np.sum, np.mean], ['sum', 'mean']), |
| 56 | + (['sum', np.mean], ['sum', 'mean']), |
| 57 | + ([np.sum, 'mean'], ['sum', 'mean']), |
| 58 | + ]) |
| 59 | + def test_agg_list_numeric_only_various_function_types(self, funcs, expected_index): |
| 60 | + """Test with different combinations of string and numpy functions.""" |
| 61 | + df = DataFrame({ |
| 62 | + 'A': [1, 2, 3], |
| 63 | + 'B': [10, 20, 30], |
| 64 | + 'C': ['a', 'b', 'c'] |
| 65 | + }) |
| 66 | + result = df.agg(funcs, numeric_only=True) |
| 67 | + expected = DataFrame({ |
| 68 | + 'A': [6.0, 2.0], |
| 69 | + 'B': [60.0, 20.0] |
| 70 | + }, index=expected_index) |
| 71 | + tm.assert_frame_equal(result, expected) |
| 72 | + |
| 73 | + @pytest.mark.parametrize("funcs", [ |
| 74 | + ['min', 'max'], |
| 75 | + ['sum', 'mean', 'std'], |
| 76 | + ['min', 'max', 'mean', 'median'], |
| 77 | + ]) |
| 78 | + def test_agg_list_numeric_only_different_function_counts(self, funcs): |
| 79 | + """Test with different numbers of functions.""" |
| 80 | + df = DataFrame({ |
| 81 | + 'A': [1, 2, 3, 4, 5], |
| 82 | + 'B': [10, 20, 30, 40, 50], |
| 83 | + 'C': ['a', 'b', 'c', 'd', 'e'] |
| 84 | + }) |
| 85 | + result = df.agg(funcs, numeric_only=True) |
| 86 | + |
| 87 | + # Verify structure |
| 88 | + assert isinstance(result, DataFrame) |
| 89 | + assert list(result.columns) == ['A', 'B'] |
| 90 | + assert list(result.index) == funcs |
| 91 | + assert result.shape == (len(funcs), 2) |
| 92 | + |
| 93 | + @pytest.mark.parametrize("data,expected_cols", [ |
| 94 | + # Only integers |
| 95 | + ({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': ['x', 'y', 'z']}, ['A', 'B']), |
| 96 | + # Only floats |
| 97 | + ({'A': [1.1, 2.2], 'B': [3.3, 4.4], 'C': ['x', 'y']}, ['A', 'B']), |
| 98 | + # Mix of int and float |
| 99 | + ({'int': [1, 2], 'float': [1.5, 2.5], 'str': ['a', 'b']}, ['int', 'float']), |
| 100 | + # Single numeric column |
| 101 | + ({'num': [1, 2, 3], 'text': ['a', 'b', 'c']}, ['num']), |
| 102 | + ]) |
| 103 | + def test_agg_list_numeric_only_various_dtypes(self, data, expected_cols): |
| 104 | + """Test with various numeric dtype combinations.""" |
| 105 | + df = DataFrame(data) |
| 106 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 107 | + |
| 108 | + assert isinstance(result, DataFrame) |
| 109 | + assert list(result.columns) == expected_cols |
| 110 | + assert list(result.index) == ['sum', 'mean'] |
| 111 | + |
| 112 | + @pytest.mark.parametrize("numeric_only", [True, False, None]) |
| 113 | + def test_agg_list_numeric_only_parameter_values(self, numeric_only): |
| 114 | + """Test with different numeric_only parameter values.""" |
| 115 | + df = DataFrame({ |
| 116 | + 'A': [1, 2, 3], |
| 117 | + 'B': [10, 20, 30] |
| 118 | + }) |
| 119 | + |
| 120 | + if numeric_only is None: |
| 121 | + result = df.agg(['sum', 'mean']) |
| 122 | + else: |
| 123 | + result = df.agg(['sum', 'mean'], numeric_only=numeric_only) |
| 124 | + |
| 125 | + expected = DataFrame({ |
| 126 | + 'A': [6, 2.0], |
| 127 | + 'B': [60, 20.0] |
| 128 | + }, index=['sum', 'mean']) |
| 129 | + tm.assert_frame_equal(result, expected) |
| 130 | + |
| 131 | + def test_agg_list_numeric_only_false_with_strings(self): |
| 132 | + """Verify numeric_only=False works with min/max on strings.""" |
| 133 | + df = DataFrame({ |
| 134 | + 'A': [1, 2, 3], |
| 135 | + 'B': ['a', 'b', 'c'] |
| 136 | + }) |
| 137 | + result = df.agg(['min', 'max'], numeric_only=False) |
| 138 | + expected = DataFrame({ |
| 139 | + 'A': [1, 3], |
| 140 | + 'B': ['a', 'c'] |
| 141 | + }, index=['min', 'max']) |
| 142 | + tm.assert_frame_equal(result, expected) |
| 143 | + |
| 144 | + def test_agg_list_numeric_only_preserves_column_order(self): |
| 145 | + """Test that column order is preserved.""" |
| 146 | + df = DataFrame({ |
| 147 | + 'Z': [1, 2, 3], |
| 148 | + 'A': [10, 20, 30], |
| 149 | + 'M': [100, 200, 300], |
| 150 | + 'text': ['a', 'b', 'c'] |
| 151 | + }) |
| 152 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 153 | + |
| 154 | + assert list(result.columns) == ['Z', 'A', 'M'] |
| 155 | + |
| 156 | + @pytest.mark.parametrize("single_func", ['sum', 'mean', 'min', 'max']) |
| 157 | + def test_agg_single_function_still_works(self, single_func): |
| 158 | + """Verify that single function (not a list) still works.""" |
| 159 | + df = DataFrame({ |
| 160 | + 'A': [1, 2, 3], |
| 161 | + 'B': [10, 20, 30], |
| 162 | + 'C': ['a', 'b', 'c'] |
| 163 | + }) |
| 164 | + result = df.agg(single_func, numeric_only=True) |
| 165 | + |
| 166 | + assert isinstance(result, pd.Series) |
| 167 | + assert 'A' in result.index |
| 168 | + assert 'B' in result.index |
| 169 | + assert 'C' not in result.index |
| 170 | + |
| 171 | + def test_agg_list_numeric_only_with_int_and_float(self): |
| 172 | + """Test that both int and float columns are included.""" |
| 173 | + df = DataFrame({ |
| 174 | + 'int_col': [1, 2, 3], |
| 175 | + 'float_col': [1.5, 2.5, 3.5], |
| 176 | + 'str_col': ['a', 'b', 'c'] |
| 177 | + }) |
| 178 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 179 | + expected = DataFrame({ |
| 180 | + 'int_col': [6.0, 2.0], |
| 181 | + 'float_col': [7.5, 2.5] |
| 182 | + }, index=['sum', 'mean']) |
| 183 | + tm.assert_frame_equal(result, expected) |
| 184 | + |
| 185 | + def test_agg_list_numeric_only_single_row(self): |
| 186 | + """Test with single row DataFrame.""" |
| 187 | + df = DataFrame({ |
| 188 | + 'A': [1], |
| 189 | + 'B': [10], |
| 190 | + 'C': ['x'] |
| 191 | + }) |
| 192 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 193 | + expected = DataFrame({ |
| 194 | + 'A': [1.0, 1.0], |
| 195 | + 'B': [10.0, 10.0] |
| 196 | + }, index=['sum', 'mean']) |
| 197 | + tm.assert_frame_equal(result, expected) |
| 198 | + |
| 199 | + # ========== NEW TESTS - Additional Edge Cases ========== |
| 200 | + |
| 201 | + def test_agg_list_numeric_only_with_nans(self): |
| 202 | + """Test DataFrame with NaN values.""" |
| 203 | + df = DataFrame({ |
| 204 | + 'A': [1, np.nan, 3], |
| 205 | + 'B': [10, 20, np.nan], |
| 206 | + 'C': ['x', 'y', 'z'] |
| 207 | + }) |
| 208 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 209 | + expected = DataFrame({ |
| 210 | + 'A': [4.0, 2.0], |
| 211 | + 'B': [30.0, 15.0] |
| 212 | + }, index=['sum', 'mean']) |
| 213 | + tm.assert_frame_equal(result, expected) |
| 214 | + |
| 215 | + def test_agg_list_numeric_only_with_datetime(self): |
| 216 | + """Test that datetime columns are excluded with numeric_only=True.""" |
| 217 | + df = DataFrame({ |
| 218 | + 'num': [1, 2, 3], |
| 219 | + 'date': pd.date_range('2020-01-01', periods=3), |
| 220 | + 'text': ['a', 'b', 'c'] |
| 221 | + }) |
| 222 | + result = df.agg(['sum', 'mean'], numeric_only=True) |
| 223 | + expected = DataFrame({ |
| 224 | + 'num': [6.0, 2.0] |
| 225 | + }, index=['sum', 'mean']) |
| 226 | + tm.assert_frame_equal(result, expected) |
| 227 | + |
| 228 | + def test_agg_list_numeric_only_large_dataframe(self): |
| 229 | + """Test with a larger DataFrame for performance verification.""" |
| 230 | + np.random.seed(42) |
| 231 | + df = DataFrame({ |
| 232 | + 'A': np.random.randint(1, 100, 1000), |
| 233 | + 'B': np.random.randn(1000), |
| 234 | + 'C': ['text'] * 1000 |
| 235 | + }) |
| 236 | + result = df.agg(['sum', 'mean', 'std'], numeric_only=True) |
| 237 | + |
| 238 | + # Just verify structure, not exact values due to randomness |
| 239 | + assert isinstance(result, DataFrame) |
| 240 | + assert list(result.columns) == ['A', 'B'] |
| 241 | + assert list(result.index) == ['sum', 'mean', 'std'] |
| 242 | + assert result.shape == (3, 2) |
0 commit comments