Starbucks
Starbucks

Reputation: 1568

How to return CUMSUM of Days in Dates - Python

How do I return the CumSum number of days for the dates provided?

import pandas as pd
df = pd.DataFrame({
    'date': ['2019-01-01','2019-01-03','2019-01-05',
             '2019-01-06','2019-01-07','2019-01-08',
             '2019-01-09','2019-01-12','2019-01-013']})

df['date'].cumsum() does not work here.

Desired dataframe:

          date  Cumsum days
0   2019-01-01            0
1   2019-01-03            2
2   2019-01-05            4
3   2019-01-06            5
4   2019-01-07            6
5   2019-01-08            7
6   2019-01-09            8
7   2019-01-12            9
8  2019-01-013           11

Upvotes: 2

Views: 194

Answers (2)

Andy L.
Andy L.

Reputation: 25239

Another way is calling diff, fillna and cumsum

df['cumsum days'] = df['date'].diff().dt.days.fillna(0).cumsum()

Out[2044]:
        date  cumsum days
0 2019-01-01          0.0
1 2019-01-03          2.0
2 2019-01-05          4.0
3 2019-01-06          5.0
4 2019-01-07          6.0
5 2019-01-08          7.0
6 2019-01-09          8.0
7 2019-01-12         11.0
8 2019-01-13         12.0

Upvotes: 1

Starbucks
Starbucks

Reputation: 1568

Thanks, this should work:

def generate_time_delta_column(df, time_column, date_first_online_column):
    return (df[time_column] - df[date_first_online_column]).dt.days

Upvotes: 1

Related Questions