w3resource

Pandas Series: expanding() function

Expanding transformations in Pandas

The expanding() function is used to provide expanding transformations.

Syntax:

Series.expanding(self, min_periods=1, center=False, axis=0)
Pandas Series expanding image

Parameters:

Name Description Type/Default Value Required / Optional
min_periods Minimum number of observations in window required to have a value (otherwise result is NA). int
Default Value : 1
Required
center Set the labels at the center of the window.

bool
Default Value : False

Required
axis If the axis is a MultiIndex (hierarchical), group by a particular level or levels.

nt or str
Default Value : 0

Required

Returns: a Window sub-classed for the particular operation

Notes: By default, the result is set to the right edge of the window. This can be changed to the center of the window by setting center=True.

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Q': [0, 2, 4, np.nan, 6]})
df

Output:

    Q
0	0.0
1	2.0
2	4.0
3	NaN
4	6.0
Pandas Series expanding image

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Q': [0, 2, 4, np.nan, 6]})
df.expanding(2).sum()

Output:

    Q
0	NaN
1	2.0
2	6.0
3	6.0
4	12.0

Previous: Rolling window calculations in Pandas
Next: Exponential weighted functions in Pandas



Share this Tutorial / Exercise on : Facebook and Twitter