Skip to main content

Accumulate Pandas series

Project description

pandas-accumulate: accumulate values along Pandas series

This package provides one function that allows accumulating values along a Pandas series very similar to cumsum and cumprod but allowing also other operators like |.

Usage examples

cumsum

Replicate cumsum:

>>> import operator
>>> import pandas as pd
>>> from pandas_accumulate import accumulate
>>> s = pd.Series([1, 2, 3])
>>> accumulate(s, operator.add)
0    1
1    3
2    6
dtype: int64

cumulative unique()

Collect the unique values cumulatively. Note: You need to pass an initial value for the accumulation in this example:

>>> s = pd.Series([1,2,1,3,2])
>>> def f(acc, v):
...     return acc | {v}
>>> accumulate(s, f, initial=set())
0          {1}
1       {1, 2}
2       {1, 2}
3    {1, 2, 3}
4    {1, 2, 3}
dtype: object

cumulative nunique()

If you're interested only in the number of unique values, just call len at the end:

>>> accumulate(s, f, initial=set()).map(len)
0    1
1    2
2    2
3    3
4    3
dtype: int64

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandas_accumulate-0.1.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

pandas_accumulate-0.1.0-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page