pandas 将字符串转换为日期 [带年份和季度]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40634813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:26:51  来源:igfitidea点击:

Convert String to Date [With Year and Quarter]

pythondatepandas

提问by hoof_hearted

I have a pandas dataframe, where one column contains a string for the year and quarter in the following format:

我有一个Pandas数据框,其中一列包含以下格式的年份和季度的字符串:

2015Q1

My Question:?How do I convert this into two datetime columns, one for the year and one for the quarter.

我的问题:?如何将其转换为两个日期时间列,一个用于年份,一个用于季度。

回答by jezrael

You can use split, then cast column yearto intand if necessary add Qto column q:

您可以使用split,然后将 column 强制转换yearint并在必要时添加Q到 column q

df = pd.DataFrame({'date':['2015Q1','2015Q2']})
print (df)
     date
0  2015Q1
1  2015Q2

df[['year','q']] = df.date.str.split('Q', expand=True)
df.year = df.year.astype(int)
df.q = 'Q' + df.q
print (df)
     date  year   q
0  2015Q1  2015  Q1
1  2015Q2  2015  Q2

Also you can use Period:

你也可以使用Period

df['date'] = pd.to_datetime(df.date).dt.to_period('Q')

df['year'] = df['date'].dt.year
df['quarter'] = df['date'].dt.quarter

print (df)
    date  year  quarter
0 2015Q1  2015        1
1 2015Q2  2015        2

回答by Julien Marrec

You could also construct a datetimeIndex and call year and quarter on it.

您还可以构造一个 datetimeIndex 并在其上调用 year 和季度。

df.index = pd.to_datetime(df.date)
df['year'] = df.index.year
df['quarter'] = df.index.quarter

              date  year  quarter
date                             
2015-01-01  2015Q1  2015        1
2015-04-01  2015Q2  2015        2

Note that you don't even need a dedicated column for year and quarter if you have a datetimeIndex, you could do a groupby like this for example: df.groupby(df.index.quarter)

请注意,如果您有 datetimeIndex,您甚至不需要年和季度的专用列,您可以执行这样的 groupby,例如: df.groupby(df.index.quarter)