3

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.