Created
May 9, 2013 17:57
-
-
Save TomAugspurger/5549256 to your computer and use it in GitHub Desktop.
Various date parsers for use with pandas' read_csv.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime as dt | |
def year_month(x): | |
ym = (int(str(x)[:4]), int(str(x)[-2:])) | |
return dt.datetime(ym[0], ym[1], 1) | |
def year(x): | |
y = int(str(x)[:4]) | |
return dt.datetime(y, 01, 1) | |
def year_month_eurostat(x): | |
year, month = (int(s) for s in x.split('M')) | |
return dt.datetime(year, month, 1) | |
def year_month_bis(x): | |
d = {'Jan': 1, | |
'Feb': 2, | |
'Mar': 3, | |
'Apr': 4, | |
'May': 5, | |
'Jun': 6, | |
'Jul': 7, | |
'Aug': 8, | |
'Sep': 9, | |
'Oct': 10, | |
'Nov': 11, | |
'Dec': 12} | |
parts = x.split('.') | |
month = d[parts[0]] | |
if parts[1].startswith(('0', '1')): | |
year = int('20' + parts[1]) | |
else: | |
year = int('19' + parts[1]) | |
return dt.datetime(year, month, 1) | |
def year_quarter_bis(x): | |
d = {'Q1': 1, | |
'Q2': 4, | |
'Q3': 7, | |
'Q4': 10} | |
parts = x.split('-') | |
year, month = int(parts[0]), d[parts[1]] | |
return dt.datetime(year, month, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment