如何更改Pandas Index的日期格式?

时间:2016-10-18 05:24:38

标签: python-3.x pandas

我正在按以下方式加载一些时间序列数据:

snp = web.DataReader("^GSPC", 'yahoo', start, end)['Adj Close']

然后索引自动格式化为'datetime64 [ns]'

然后我将每日数据重新采样为每年一次:

snp_yr = snp.resample('A')

日期格式仍然与已描述的相同。如何将其更改为仅年份(%Y)?

E.g。从'2015-12-31 00:00:00'到'2015'

1 个答案:

答案 0 :(得分:0)

我认为您需要DatetimeIndex.year然后如果需要转换为string添加astype

df.index = df.index.year

样品:

start = pd.to_datetime('2015-02-24')
rng = pd.date_range(start, periods=10, freq='3M')

df = pd.DataFrame({'a': range(10)},index=rng) 
print (df)
            a
2015-02-28  0
2015-05-31  1
2015-08-31  2
2015-11-30  3
2016-02-29  4
2016-05-31  5
2016-08-31  6
2016-11-30  7
2017-02-28  8
2017-05-31  9

df.index = df.index.year.astype(str)
print (df)
      a
2015  0
2015  1
2015  2
2015  3
2016  4
2016  5
2016  6
2016  7
2017  8
2017  9

print (df.index)
Index(['2015', '2015', '2015', '2015', '2016', '2016', '2016', '2016', '2017',
       '2017'],
      dtype='object')

strftime的另一个解决方案:

df.index = df.index.strftime('%Y')
print (df)
      a
2015  0
2015  1
2015  2
2015  3
2016  4
2016  5
2016  6
2016  7
2017  8
2017  9

print (df.index)
Index(['2015', '2015', '2015', '2015', '2016', '2016', '2016', '2016', '2017',
       '2017'],
      dtype='object')