文档指定strftime
on Period数据类型不Datetime数据类型;使用%q
格式化程序,您可以将日期时间索引转换为周期(以天为单位),然后对其进行格式化:
cols = pd.DatetimeIndex(['2000-01-01', '2000-04-01', '2000-07-01', '2000-10-01',
'2001-01-01', '2001-04-01', '2001-07-01', '2001-10-01',
'2002-01-01', '2002-04-01', '2002-07-01', '2002-10-01'])
cols.to_period('D').strftime('%Yq%q')
# hs.columns.to_period('D').strftime('%Yq%q')
#array([u'2000q1', u'2000q2', u'2000q3', u'2000q4', u'2001q1', u'2001q2',
# u'2001q3', u'2001q4', u'2002q1', u'2002q2', u'2002q3', u'2002q4'],
# dtype='<U6')
或者简单地使用to_period
with Q
(季度)为单位:
cols.to_period('Q')
# hs.columns.to_period('Q')
#PeriodIndex(['2000Q1', '2000Q2', '2000Q3', '2000Q4', '2001Q1', '2001Q2',
# '2001Q3', '2001Q4', '2002Q1', '2002Q2', '2002Q3', '2002Q4'],
# dtype='period[Q-DEC]', freq='Q-DEC')