Reputation: 468
I have a dataframe like:
Date Value
2014-11-03 8767.83
2014-12-01 8948.96
2015-01-02 9098.98
2015-02-02 8774.11
2015-03-02 9102.82
2015-04-01 8672.13
2015-05-01 8737.00
2015-06-01 8394.23
2015-07-01 8103.86
2015-08-03 8416.86
2015-09-01 7677.32
2015-10-01 7832.70
I want all the dates to be the 1st of the month like:
Date Value
2014-11-01 8767.83
2014-12-01 8948.96
2015-01-01 9098.98
2015-02-01 8774.11
2015-03-01 9102.82
2015-04-01 8672.13
2015-05-01 8737.00
2015-06-01 8394.23
2015-07-01 8103.86
2015-08-01 8416.86
2015-09-01 7677.32
2015-10-01 7832.70
I know this has to be really simple, but I'm new at pandas/python and looked for over an hour at this point. Thanks in advance.
Upvotes: 1
Views: 2145
Reputation: 4234
I know this is an old question, but in case you want to create a monthly start index from scratch, you can make use of Panda's offset-alias functionality to do so.
import pandas as pd
monthly_start_index = pd.date_range(start='2018-01-01',
stop='2018-12-01',
freq = 'MS')
df.index = monthly_start_index
Upvotes: 0
Reputation: 20553
You may just use simple list comprehension to "convert" and re-assign your index:
In [3]: df
Out[3]:
Value
Date
2014-11-03 8767.83
2014-12-01 8948.96
2015-01-02 9098.98
2015-02-02 8774.11
2015-03-02 9102.82
2015-04-01 8672.13
2015-05-01 8737.00
2015-06-01 8394.23
2015-07-01 8103.86
2015-08-03 8416.86
2015-09-01 7677.32
2015-10-01 7832.70
In [4]: df.index = [pd.datetime(x.year, x.month, 1) for x in df.index.tolist()]
In [5]: df
Out[5]:
Value
2014-11-01 8767.83
2014-12-01 8948.96
2015-01-01 9098.98
2015-02-01 8774.11
2015-03-01 9102.82
2015-04-01 8672.13
2015-05-01 8737.00
2015-06-01 8394.23
2015-07-01 8103.86
2015-08-01 8416.86
2015-09-01 7677.32
2015-10-01 7832.70
Upvotes: 2