Jason
Jason

Reputation: 2884

Resample dataframe with specific start/end dates along with a groupby

I have some transaction data that looks like this.

import pandas as pd
from io import StringIO
from datetime import datetime
from datetime import timedelta

data = """\
cust_id,datetime,txn_type,txn_amt
100,2019-03-05 6:30,Credit,25000
100,2019-03-06 7:42,Debit,4000
100,2019-03-07 8:54,Debit,1000
101,2019-03-05 5:32,Credit,25000
101,2019-03-06 7:13,Debit,5000
101,2019-03-06 8:54,Debit,2000
"""

df = pd.read_table(StringIO(data), sep=',')
df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H:%M:%S')
# use datetime as the dataframe index
df = df.set_index('datetime')
print(df)

                    cust_id txn_type  txn_amt
datetime                                      
2019-03-05 06:30:00      100   Credit    25000
2019-03-06 07:42:00      100    Debit     4000
2019-03-07 08:54:00      100    Debit     1000
2019-03-05 05:32:00      101   Credit    25000
2019-03-06 07:13:00      101    Debit     5000
2019-03-06 08:54:00      101    Debit     2000

I would like to resample the data at the daily level aggregating (summing) txn_amount for each combination of cust_id and txn_type. At the same time, I want to standardize the index to 5 days (currently the data only contains 3 days of data). In essence, this is what I would like to produce:

             cust_id txn_type  txn_amt
datetime    
2019-03-03    100    Credit   0
2019-03-03    100    Debit    0
2019-03-03    101    Credit   0
2019-03-03    101    Debit    0
2019-03-04    100    Credit   0
2019-03-04    100    Debit    0
2019-03-04    101    Credit   0
2019-03-04    101    Debit    0
2019-03-05    100    Credit   25000
2019-03-05    100    Debit    0
2019-03-05    101    Credit   25000
2019-03-05    101    Debit    0
2019-03-06    100    Credit   0
2019-03-06    100    Debit    4000
2019-03-06    101    Credit   0
2019-03-06    101    Debit    7000   => (note: aggregated value)
2019-03-07    100    Credit   0
2019-03-07    100    Debit    1000
2019-03-07    101    Credit   0
2019-03-07    101    Debit    0

So far, I've tried creating a new datetime index and to try and resample and then use the newly created index like so:

# create a 5 day datetime index
end_dt = max(df.index).to_pydatetime().strftime('%Y-%m-%d')
start_dt = max(df.index) - timedelta(days=4)
start_dt = start_dt.to_pydatetime().strftime('%Y-%m-%d')
dt_index = pd.date_range(start=start_dt, end=end_dt, freq='1D', name='datetime')

However, I am not sure how to go about the grouping part. Resampling with no grouping outputs wrong results:

# resample timeseries so that one row is 1 day's worth of txns
df2 = df.resample(rule='D').sum().reindex(dt_index).fillna(0)
print(df2)
            cust_id  txn_amt
datetime                    
2019-03-03      0.0      0.0
2019-03-04      0.0      0.0
2019-03-05    201.0  50000.0
2019-03-06    302.0  11000.0
2019-03-07    100.0   1000.0

So, how can I incorporate a grouping of cust_id and tsn_type when resampling? I have seen this similar question but the op's data structure is different.

Upvotes: 2

Views: 341

Answers (1)

BENY
BENY

Reputation: 323306

I am using reindex here , the key is to setting up the Multiple index

df.index=pd.to_datetime(df.index).date
df=df.groupby([df.index,df['txn_type'],df['cust_id']]).agg({'txn_amt':'sum'}).reset_index(level=[1,2])
drange=pd.date_range(end=df.index.max(),periods =5)
idx=pd.MultiIndex.from_product([drange,df.cust_id.unique(),df.txn_type.unique()])
Newdf=df.set_index(['cust_id','txn_type'],append=True).reindex(idx,fill_value=0).reset_index(level=[1,2])
Newdf
Out[749]: 
            level_1 level_2  txn_amt
2019-03-03      100  Credit        0
2019-03-03      100   Debit        0
2019-03-03      101  Credit        0
2019-03-03      101   Debit        0
2019-03-04      100  Credit        0
2019-03-04      100   Debit        0
2019-03-04      101  Credit        0
2019-03-04      101   Debit        0
2019-03-05      100  Credit    25000
2019-03-05      100   Debit        0
2019-03-05      101  Credit    25000
2019-03-05      101   Debit        0
2019-03-06      100  Credit        0
2019-03-06      100   Debit     4000
2019-03-06      101  Credit        0
2019-03-06      101   Debit     7000
2019-03-07      100  Credit        0
2019-03-07      100   Debit     1000
2019-03-07      101  Credit        0
2019-03-07      101   Debit        0

Upvotes: 3

Related Questions