user2123288
user2123288

Reputation: 1119

How to resample pandas series monthy (not end, not start)?

I know how to resample a dataframe but feel frustrated by rule parameter not accepting DateOffset(months=1).

How can I resample with monthly frequency but from any give date. For example:

    ts = pd.date_range(end='19/9/2015', periods=127, freq='D')
    df = pd.DataFrame(pd.Series(range(len(ts)), index=ts))
    print(df.resample('M',how='sum',convention='end').tail(3))

will give me

               0
2015-07-31  1674
2015-08-31  2635
2015-09-30  2090

where I wanted it to give

               0
2015-07-19  ...
2015-08-19  ...
2015-09-19  ...

How can I achieve this ?

Upvotes: 0

Views: 829

Answers (2)

maxymoo
maxymoo

Reputation: 36545

This is sort of janky but I think it works:

df.index = df.index + pd.to_timedelta('19D')

print(df2.resample('M',how='sum',loffset= '19D').tail(3))

               0
2015-09-19  1674
2015-10-19  2535
2015-11-19  3051

Upvotes: 1

chrisb
chrisb

Reputation: 52246

There's an older issue about this

Here's one workaround - you can convert to a monthly PeriodIndex - roll the month forward if past the day of month (the where(...) below) then add back the number of days to get your labels.

In [61]: day = 19

In [62]: p_months = df.index.to_period('M')

In [66]: month = (p_months + np.where(df.index.day > day, 1, 0)).to_timestamp(how='s')

In [67]: grouper = month + pd.Timedelta(days=day-1)

In [68]: df.groupby(grouper).sum()
Out[68]: 
               0
2015-05-19     6
2015-06-19   589
2015-07-19  1485
2015-08-19  2480
2015-09-19  3441

Upvotes: 0

Related Questions