【python数据处理(1)】如何将pandas中的Periodindex中的时间数据提取出来,并转化成时间数值列表数据?

您所在的位置:网站首页 python把日期数据转换成数字 【python数据处理(1)】如何将pandas中的Periodindex中的时间数据提取出来,并转化成时间数值列表数据?

【python数据处理(1)】如何将pandas中的Periodindex中的时间数据提取出来,并转化成时间数值列表数据?

2023-09-11 09:00| 来源: 网络整理| 查看: 265

import numpy as np import pandas as pd import time

df = pd.DataFrame({'rate':np.random.randn(10)},index = pd.period_range('2001','2010',freq = 'Y')) df.index= df.index.to_timestamp()#需要先将period数据转化为时间戳 ls_year = [time.strftime('%Y',df.index.to_pydatetime()[i].timetuple()) for i in range(len(df.index))] df.index = ls_year df.index.name = 'year'

from bokeh.plotting import figure,show from bokeh.io import output_notebook output_notebook() from bokeh.models import ColumnDataSource from bokeh.models import HoverTool

hover = HoverTool(tooltips=[('年份','@year'),                             ('增长率','@rate')])

source = ColumnDataSource(df)

p = figure(plot_width = 800, plot_height=340,title = '增长率',x_axis_label = '年份',y_axis_label = '增长率',x_range = ls_year,           tools = [hover, 'box_select ,xwheel_zoom, reset,pan,crosshair'])

p.line(x = 'year', y = 'rate', source = source)

show(p)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3