|
10 | 10 | overload, |
11 | 11 | ) |
12 | 12 |
|
13 | | -from pandas.util._decorators import ( |
14 | | - Appender, |
15 | | - Substitution, |
16 | | -) |
17 | | - |
18 | 13 | from pandas.core.indexers.objects import ( |
19 | 14 | BaseIndexer, |
20 | 15 | ExpandingIndexer, |
21 | 16 | GroupbyIndexer, |
22 | 17 | ) |
23 | | -from pandas.core.window.doc import ( |
24 | | - template_pipe, |
25 | | -) |
26 | 18 | from pandas.core.window.rolling import ( |
27 | 19 | BaseWindowGroupby, |
28 | 20 | RollingAndExpandingMixin, |
@@ -343,35 +335,90 @@ def pipe( |
343 | 335 | ) -> T: ... |
344 | 336 |
|
345 | 337 | @final |
346 | | - @Substitution( |
347 | | - klass="Expanding", |
348 | | - examples=""" |
349 | | - >>> df = pd.DataFrame({'A': [1, 2, 3, 4]}, |
350 | | - ... index=pd.date_range('2012-08-02', periods=4)) |
351 | | - >>> df |
352 | | - A |
353 | | - 2012-08-02 1 |
354 | | - 2012-08-03 2 |
355 | | - 2012-08-04 3 |
356 | | - 2012-08-05 4 |
357 | | -
|
358 | | - To get the difference between each expanding window's maximum and minimum |
359 | | - value in one pass, you can do |
360 | | -
|
361 | | - >>> df.expanding().pipe(lambda x: x.max() - x.min()) |
362 | | - A |
363 | | - 2012-08-02 0.0 |
364 | | - 2012-08-03 1.0 |
365 | | - 2012-08-04 2.0 |
366 | | - 2012-08-05 3.0""", |
367 | | - ) |
368 | | - @Appender(template_pipe) |
369 | 338 | def pipe( |
370 | 339 | self, |
371 | 340 | func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], |
372 | 341 | *args: Any, |
373 | 342 | **kwargs: Any, |
374 | 343 | ) -> T: |
| 344 | + """ |
| 345 | + Apply a ``func`` with arguments to this Expanding object and return its result. |
| 346 | +
|
| 347 | + Use `.pipe` when you want to improve readability by chaining together |
| 348 | + functions that expect Series, DataFrames, GroupBy, Rolling, Expanding or |
| 349 | + Resampler |
| 350 | + objects. |
| 351 | + Instead of writing |
| 352 | +
|
| 353 | + >>> h = lambda x, arg2, arg3: x + 1 - arg2 * arg3 |
| 354 | + >>> g = lambda x, arg1: x * 5 / arg1 |
| 355 | + >>> f = lambda x: x**4 |
| 356 | + >>> df = pd.DataFrame( |
| 357 | + ... {"A": [1, 2, 3, 4]}, |
| 358 | + index=pd.date_range("2012-08-02", periods=4) |
| 359 | + ... ) |
| 360 | + >>> h(g(f(df.rolling("2D")), arg1=1), arg2=2, arg3=3) # doctest: +SKIP |
| 361 | +
|
| 362 | + You can write |
| 363 | +
|
| 364 | + >>> ( |
| 365 | + ... df.rolling("2D").pipe(f).pipe(g, arg1=1).pipe(h, arg2=2, arg3=3) |
| 366 | + ... ) # doctest: +SKIP |
| 367 | +
|
| 368 | + which is much more readable. |
| 369 | +
|
| 370 | + Parameters |
| 371 | + ---------- |
| 372 | + func : callable or tuple of (callable, str) |
| 373 | + Function to apply to this Expanding object or, alternatively, |
| 374 | + a `(callable, data_keyword)` tuple where `data_keyword` is a |
| 375 | + string indicating the keyword of `callable` that expects the |
| 376 | + Expanding object. |
| 377 | + *args : iterable, optional |
| 378 | + Positional arguments passed into `func`. |
| 379 | + **kwargs : dict, optional |
| 380 | + A dictionary of keyword arguments passed into `func`. |
| 381 | +
|
| 382 | + Returns |
| 383 | + ------- |
| 384 | + Expanding |
| 385 | + The original object with the function `func` applied. |
| 386 | +
|
| 387 | + See Also |
| 388 | + -------- |
| 389 | + Series.pipe : Apply a function with arguments to a series. |
| 390 | + DataFrame.pipe: Apply a function with arguments to a dataframe. |
| 391 | + apply : Apply function to each group instead of to the |
| 392 | + full Expanding object. |
| 393 | +
|
| 394 | + Notes |
| 395 | + ----- |
| 396 | + See more `here |
| 397 | + <https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html#piping-function-calls>`_ |
| 398 | +
|
| 399 | + Examples |
| 400 | + -------- |
| 401 | +
|
| 402 | + >>> df = pd.DataFrame( |
| 403 | + ... {"A": [1, 2, 3, 4]}, index=pd.date_range("2012-08-02", periods=4) |
| 404 | + ... ) |
| 405 | + >>> df |
| 406 | + A |
| 407 | + 2012-08-02 1 |
| 408 | + 2012-08-03 2 |
| 409 | + 2012-08-04 3 |
| 410 | + 2012-08-05 4 |
| 411 | +
|
| 412 | + To get the difference between each expanding window's maximum and minimum |
| 413 | + value in one pass, you can do |
| 414 | +
|
| 415 | + >>> df.expanding().pipe(lambda x: x.max() - x.min()) |
| 416 | + A |
| 417 | + 2012-08-02 0.0 |
| 418 | + 2012-08-03 1.0 |
| 419 | + 2012-08-04 2.0 |
| 420 | + 2012-08-05 3.0 |
| 421 | + """ |
375 | 422 | return super().pipe(func, *args, **kwargs) |
376 | 423 |
|
377 | 424 | def sum( |
|
0 commit comments