Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.core.resample.Resampler.pipe#

finalResampler.pipe(func,*args,**kwargs)[source]#

Apply afunc with arguments to this Resampler object and return its result.

Use.pipe when you want to improve readability by chaining togetherfunctions that expect Series, DataFrames, GroupBy or Resampler objects.Instead of writing

>>>h=lambdax,arg2,arg3:x+1-arg2*arg3>>>g=lambdax,arg1:x*5/arg1>>>f=lambdax:x**4>>>df=pd.DataFrame([["a",4],["b",5]],columns=["group","value"])>>>h(g(f(df.groupby('group')),arg1=1),arg2=2,arg3=3)

You can write

>>>(df.groupby('group')....pipe(f)....pipe(g,arg1=1)....pipe(h,arg2=2,arg3=3))

which is much more readable.

Parameters:
funccallable or tuple of (callable, str)

Function to apply to this Resampler object or, alternatively,a(callable, data_keyword) tuple wheredata_keyword is astring indicating the keyword ofcallable that expects theResampler object.

argsiterable, optional

Positional arguments passed intofunc.

kwargsdict, optional

A dictionary of keyword arguments passed intofunc.

Returns:
the return type offunc.

See also

Series.pipe

Apply a function with arguments to a series.

DataFrame.pipe

Apply a function with arguments to a dataframe.

apply

Apply function to each group instead of to the full Resampler object.

Notes

See morehere

Examples

>>>df=pd.DataFrame({'A':[1,2,3,4]},...index=pd.date_range('2012-08-02',periods=4))>>>df            A2012-08-02  12012-08-03  22012-08-04  32012-08-05  4

To get the difference between each 2-day period’s maximum and minimumvalue in one pass, you can do

>>>df.resample('2D').pipe(lambdax:x.max()-x.min())            A2012-08-02  12012-08-04  1

[8]ページ先頭

©2009-2025 Movatter.jp