EDIT:
If you're coming to this question and your string looks like 1996-Q1
, then just use pd.to_datetime(df['Quarter'])
to convert it to a proper pandas datetime. This question is about solving all the quarter dates that are not in this standard format.
ORIGINAL QUESTION:
I'm looking for a nice, readable and understandable way (one that you can remember for the next time) to convert Q3 1996
to a pandas datetime, for example 1996-07-01
in this case.
Until now I found this, but it's mighty ugly:
df = pd.DataFrame({'Quarter':['Q3 1996', 'Q4 1996', 'Q1 1997']})
?
df['date'] = (
pd.to_datetime(
df['Quarter'].str.split(' ').apply(lambda x: ''.join(x[::-1]))
))
?
print(df)
Quarter date
0 Q3 1996 1996-07-01
1 Q4 1996 1996-10-01
2 Q1 1997 1997-01-01
I was hoping the following would work, because it's readable, but unfortunately it doesn't:
df['date'] = pd.to_datetime(df['Quarter'], format='%q %Y')
The problem is also that quarter and year are apparently in the wrong order for pandas to do simple processing.
Can anyone help me find a cleaner way of converting Q3 1996
to a pandas datetime?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…