gpt4 book ai didi

python - Plotly:如何处理金融时间序列的缺失日期?

转载 作者:行者123 更新时间:2023-12-02 16:28:02 24 4
gpt4 key购买 nike

金融时间序列通常充满缺失数据。开箱即用,通过仅显示如下所示的一行,以可视化方式处理带有缺失时间戳的系列。但这里的挑战在于,将时间戳解释为一个值,并在图中插入所有缺失的日期。

enter image description here

在大多数情况下,我发现完全忽略这些日期会使 plotly 看起来更好。https://plotly.com/python/time-series/#hiding-weekends-and-holidays 下的 plotly 文档中的示例显示如何使用以下方法处理一些日期类别(如周末或假期)的缺失日期:

fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
dict(values=["2015-12-25", "2016-01-01"]) # hide Christmas and New Year's
]
)

这里的缺点是您的数据集也可能缺少任何其他工作日的一些数据。当然,您必须为不同国家/地区指定特定的假期日期,那么还有其他方法吗?

可重现代码:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

# data
np.random.seed(1234)
n_obs = 15
frequency = 'D'
daterange = pd.date_range('2020', freq=frequency, periods=n_obs)
values = np.random.randint(low=-5, high=6, size=n_obs).tolist()
df = pd.DataFrame({'time':daterange, 'value':values})
df = df.set_index('time')
df.iloc[0]=100; df['value']=df.value.cumsum()

# Missing timestamps
df.iloc[2:5] = np.nan; df.iloc[8:13] = np.nan
df.dropna(inplace = True)

# plotly figure
fig=go.Figure(go.Scatter(x=df.index, y =df['value']))
fig.update_layout(template = 'plotly_dark')
fig.show()

最佳答案

他们这里的关键还是要用rangebreak属性。但是,如果您要遵循链接示例中解释的方法,则必须手动包含每个缺失的日期。但这种情况下丢失数据的解决方案实际上是更多丢失数据。这就是为什么:

1.您可以retrieve the timestamps从系列的开头和结尾,然后

2.构建complete timeline在那个时期内(可能有更多缺失的日期)使用:

dt_all = pd.date_range(start=df.index[0],
end=df.index[-1],
freq = 'D')

3. 接下来可以isolate the timestampsdf.index 不在该时间线中使用:

dt_breaks = [d for d in dt_all_py if d not in dt_obs_py]

4. 最后,您可以在 rangebreaks 中包含这些时间戳像这样:

fig.update_xaxes(
rangebreaks=[dict(values=dt_breaks)]
)

plotly :

enter image description here

完整代码:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

# data
np.random.seed(1234)
n_obs = 15
frequency = 'D'
daterange = pd.date_range('2020', freq=frequency, periods=n_obs)
values = np.random.randint(low=-5, high=6, size=n_obs).tolist()
df = pd.DataFrame({'time':daterange, 'value':values})
df = df.set_index('time')
df.iloc[0]=100; df['value']=df.value.cumsum()

# Missing timestamps
df.iloc[2:5] = np.nan; df.iloc[8:13] = np.nan
df.dropna(inplace = True)

# plotly figure
fig=go.Figure(go.Scatter(x=df.index, y =df['value']))
fig.update_layout(template = 'plotly_dark')

# complete timeline between first and last timestamps
dt_all = pd.date_range(start=df.index[0],
end=df.index[-1],
freq = frequency)

# make sure input and synthetic time series are of the same types
dt_all_py = [d.to_pydatetime() for d in dt_all]
dt_obs_py = [d.to_pydatetime() for d in df.index]

# find which timestamps are missing in the complete timeline
dt_breaks = [d for d in dt_all_py if d not in dt_obs_py]

# remove missing timestamps from visualization
fig.update_xaxes(
rangebreaks=[dict(values=dt_breaks)] # hide timestamps with no values
)
#fig.update_layout(title=dict(text="Some dates are missing, but still displayed"))
fig.update_layout(title=dict(text="Missing dates are excluded by rangebreaks"))
fig.update_xaxes(showgrid=False)
fig.show()

关于python - Plotly:如何处理金融时间序列的缺失日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64018267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com