|
2 | 2 | test .agg behavior / note that .apply is tested generally in test_groupby.py
|
3 | 3 | """
|
4 | 4 | import functools
|
| 5 | +from functools import partial |
5 | 6 |
|
6 | 7 | import numpy as np
|
7 | 8 | import pytest
|
8 | 9 |
|
| 10 | +from pandas.errors import PerformanceWarning |
| 11 | + |
9 | 12 | from pandas.core.dtypes.common import is_integer_dtype
|
10 | 13 |
|
11 | 14 | import pandas as pd
|
@@ -252,6 +255,61 @@ def test_agg_multiple_functions_maintain_order(df):
|
252 | 255 | tm.assert_index_equal(result.columns, exp_cols)
|
253 | 256 |
|
254 | 257 |
|
| 258 | +def test_agg_multiple_functions_same_name(): |
| 259 | + # GH 30880 |
| 260 | + df = pd.DataFrame( |
| 261 | + np.random.randn(1000, 3), |
| 262 | + index=pd.date_range("1/1/2012", freq="S", periods=1000), |
| 263 | + columns=["A", "B", "C"], |
| 264 | + ) |
| 265 | + result = df.resample("3T").agg( |
| 266 | + {"A": [partial(np.quantile, q=0.9999), partial(np.quantile, q=0.1111)]} |
| 267 | + ) |
| 268 | + expected_index = pd.date_range("1/1/2012", freq="3T", periods=6) |
| 269 | + expected_columns = MultiIndex.from_tuples([("A", "quantile"), ("A", "quantile")]) |
| 270 | + expected_values = np.array( |
| 271 | + [df.resample("3T").A.quantile(q=q).values for q in [0.9999, 0.1111]] |
| 272 | + ).T |
| 273 | + expected = pd.DataFrame( |
| 274 | + expected_values, columns=expected_columns, index=expected_index |
| 275 | + ) |
| 276 | + tm.assert_frame_equal(result, expected) |
| 277 | + |
| 278 | + |
| 279 | +def test_agg_multiple_functions_same_name_with_ohlc_present(): |
| 280 | + # GH 30880 |
| 281 | + # ohlc expands dimensions, so different test to the above is required. |
| 282 | + df = pd.DataFrame( |
| 283 | + np.random.randn(1000, 3), |
| 284 | + index=pd.date_range("1/1/2012", freq="S", periods=1000), |
| 285 | + columns=["A", "B", "C"], |
| 286 | + ) |
| 287 | + result = df.resample("3T").agg( |
| 288 | + {"A": ["ohlc", partial(np.quantile, q=0.9999), partial(np.quantile, q=0.1111)]} |
| 289 | + ) |
| 290 | + expected_index = pd.date_range("1/1/2012", freq="3T", periods=6) |
| 291 | + expected_columns = pd.MultiIndex.from_tuples( |
| 292 | + [ |
| 293 | + ("A", "ohlc", "open"), |
| 294 | + ("A", "ohlc", "high"), |
| 295 | + ("A", "ohlc", "low"), |
| 296 | + ("A", "ohlc", "close"), |
| 297 | + ("A", "quantile", "A"), |
| 298 | + ("A", "quantile", "A"), |
| 299 | + ] |
| 300 | + ) |
| 301 | + non_ohlc_expected_values = np.array( |
| 302 | + [df.resample("3T").A.quantile(q=q).values for q in [0.9999, 0.1111]] |
| 303 | + ).T |
| 304 | + expected_values = np.hstack([df.resample("3T").A.ohlc(), non_ohlc_expected_values]) |
| 305 | + expected = pd.DataFrame( |
| 306 | + expected_values, columns=expected_columns, index=expected_index |
| 307 | + ) |
| 308 | + # PerformanceWarning is thrown by `assert col in right` in assert_frame_equal |
| 309 | + with tm.assert_produces_warning(PerformanceWarning): |
| 310 | + tm.assert_frame_equal(result, expected) |
| 311 | + |
| 312 | + |
255 | 313 | def test_multiple_functions_tuples_and_non_tuples(df):
|
256 | 314 | # #1359
|
257 | 315 | funcs = [("foo", "mean"), "std"]
|
|
0 commit comments