Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i resample a pandas DatetimeIndex by 1st of month

i have some data like this

df = pd.DataFrame(index = pd.date_range('01/27/2018',periods = 290,freq = 'H'))
df['v'] = 1
In [85]: df
Out[85]:
                     v
2018-01-27 00:00:00  1
2018-01-27 01:00:00  1
2018-01-27 02:00:00  1
2018-01-27 03:00:00  1
2018-01-27 04:00:00  1
2018-01-27 05:00:00  1
2018-01-27 06:00:00  1
2018-01-27 07:00:00  1
2018-01-27 08:00:00  1
2018-01-27 09:00:00  1
2018-01-27 10:00:00  1
2018-01-27 11:00:00  1
2018-01-27 12:00:00  1
...                 ..
2018-02-07 12:00:00  1
2018-02-07 13:00:00  1
2018-02-07 14:00:00  1
2018-02-07 15:00:00  1
2018-02-07 16:00:00  1
2018-02-07 17:00:00  1
2018-02-07 18:00:00  1
2018-02-07 19:00:00  1
2018-02-07 20:00:00  1
2018-02-07 21:00:00  1
2018-02-07 22:00:00  1
2018-02-07 23:00:00  1
2018-02-08 00:00:00  1
2018-02-08 01:00:00  1

[290 rows x 1 columns]

and i resample by month, the index show Last day of last month, i want the index show the 1st of the month

In [87]: df.resample('M', how='sum')
Out[87]:
              v
2018-01-31  120
2018-02-28  170

The effect I want is like this, so how can i write my code

Out[87]:
                  v
    2018-01-01  120
    2018-02-01  170
like image 871
yongjian su Avatar asked Dec 04 '22 19:12

yongjian su


1 Answers

As @sacul mentioned in comment, go with MS.

Available options:

B       business day frequency
C       custom business day frequency (experimental)
D       calendar day frequency
W       weekly frequency
M       month end frequency
SM      semi-month end frequency (15th and end of month)
BM      business month end frequency
CBM     custom business month end frequency
MS      month start frequency
SMS     semi-month start frequency (1st and 15th)
BMS     business month start frequency
CBMS    custom business month start frequency
Q       quarter end frequency
BQ      business quarter endfrequency
QS      quarter start frequency
BQS     business quarter start frequency
A       year end frequency
BA      business year end frequency
AS      year start frequency
BAS     business year start frequency
BH      business hour frequency
H       hourly frequency
T       minutely frequency
S       secondly frequency
L       milliseonds
U       microseconds
N       nanoseconds

Reference: Offset Aliases

like image 198
meW Avatar answered Dec 28 '22 23:12

meW