Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas.read_csv parse_dates without the day

I'm reading a csv file with this structure:

2008,1,283.7,8
2008,2,323.1,8
2008,3,270.7,2
2008,4,353.6,2

year, month, data, observation

I'm reading it with read_csv function by this way:

df_in = pd.read_csv(file, header=None, index_col='Date', parse_dates={'Date': [0, 1]})

There is not any problem with the code, just that the day of all dates is the day when I run the code, i.e., today is april 26, and the index of the example is:

                   2  3
   Date               
   2008-01-26  283.7  8
   2008-02-26  323.1  8
   2008-03-26  270.7  2
   2008-04-26  353.6  2

I need that the day of the index to be the first of each month.

                  2  3
   Date               
   2008-01-1  283.7  8
   2008-02-1  323.1  8
   2008-03-1  270.7  2
   2008-04-1  353.6  2

Thanks for help me.

like image 609
anvelascos Avatar asked May 17 '26 14:05

anvelascos


1 Answers

I'd do the following:

data = """
year,month,x1,x2
2008,1,283.7,8
2008,2,323.1,8
2008,3,270.7,2
2008,4,353.6,2
""" 

df = pd.read_csv(StringIO(data),header=True,                           
                 parse_dates={'date':[0,1]},
                 index_col='date')
df.index = df.index.values.astype('datetime64[M]')
print df

               x1  x2
2008-01-01  283.7   8
2008-02-01  323.1   8
2008-03-01  270.7   2
2008-04-01  353.6   2
like image 152
Karl D. Avatar answered May 20 '26 03:05

Karl D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!