0

I have the following pandas data frame loaded from a csv file, with the first column (yearly quarter) as displayed.

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 44 entries, 0 to 43
Data columns (total 2 columns):
Quarter     44 non-null object
Earnings    44 non-null float64
dtypes: float64(1), object(1)
memory usage: 784.0+ bytes
None

 Quarter  Earnings
 0  2007Q1     -0.36
 1  2007Q2     -0.38
 2  2007Q3      0.07
 3  2007Q4      1.85
 4  2008Q1     -0.34

I would like the first column transformed into this (below). Is there some type of python or pandas datetime function to do this

In [1]: print(HRB)
            Earnings
Quarter             
2007-01-01     -0.36
2007-04-01     -0.38
2007-07-01      0.07
2007-10-01      1.85
2008-01-01     -0.34
2008-04-01     -0.42
2008-07-01      0.02

Here is the raw csv data

Quarter,Earnings
2007Q1,-0.36
2007Q2,-0.38
2007Q3,0.07
2007Q4,1.85
2008Q1,-0.34
2008Q2,-0.42
2008Q3,0.02

1 Answers1

0

Assuming the yearly quarter is contained in the column named quarter in the dataframe data, below code converts them to datetime objects.

print(data)

    quarter
0   2007Q1
1   2007Q2
2   2007Q3
3   2007Q4
4   2008Q1

pd.to_datetime(data['quarter'])

0   2007-01-01
1   2007-04-01
2   2007-07-01
3   2007-10-01
4   2008-01-01

This answer should give you more details.

harvpan
  • 8,571
  • 2
  • 18
  • 36
  • When I tried your answer it throws this error, I edited the question to give more detail on the data frame and raw data.AttributeError Traceback (most recent call last) ----> 1 HRB.to_datetime(data['quarter']) AttributeError: 'DataFrame' object has no attribute 'to_datetime' – Christopher Pfeifer Apr 09 '18 at 17:34
  • 1
    O.K. I figured out how to use the pd.to_datetime() command you answered with, and (yes) it worked great. Thank you. – Christopher Pfeifer Apr 09 '18 at 17:49
  • Glad, I could help. – harvpan Apr 09 '18 at 18:27