Does Pandas support quarterly dates of the form yyyyQp (e.g. 2013Q2)?

I import CSV macroeconomic data and could not figure out how to get Pandas to interpret this type of date. Is there a way to do this automatically, or do I need to parse it myself?

When I ask the parser to try, I get:

File "datetime.pxd", line 133, in datetime._string_to_dts (pandas/tslib.c:31399)ValueError: Unable to parse 2002Q1 
+8
python pandas
source share
3 answers

Since pd.Period can analyze quarterly periods, you can use it as a custom date_parser . Then, to convert the date to the last day of the quarter, you can use the map attribute and end_time :

 import pandas as pd text = '''\ date val 2013Q2 100 2013Q3 120 ''' filename = '/tmp/data' with open(filename, 'w') as f: f.write(text) df = pd.read_table(filename, sep='\s+', date_parser=pd.Period, parse_dates=[0]) df['date'] = df['date'].map(lambda x: x.end_time.date()) print(df) # date val # 0 2013-06-30 100 # 1 2013-09-30 120 
+7
source share

It has very good support for all the various business dates / times and frequencies. But you probably have to parse this particular format yourself.

0
source share

Here is something to help those who have years and quarters in different columns:

 year quarter foo 1994 q1 10 1994 q3 20 1995 q1 30 1995 q3 40 

The parse_dates argument for read_csv only works. It is very cool:

 >>> pd.read_csv('bar.csv', parse_dates={'period':['year', 'quarter']}) period foo 1994 q1 10 1994 q3 20 1995 q1 30 1995 q3 40 
0
source share

All Articles