gpt4 book ai didi

python - 如何从现在开始拆分最近 7 天的日期

转载 作者:太空宇宙 更新时间:2023-11-04 04:20:29 24 4
gpt4 key购买 nike

我想从现在开始拆分最后 7 天的日期。

这是我的代码。

df = pdr.get_data_yahoo('ibm',
start=datetime.datetime(y-5, m, d),
end=pd.datetime.now().date())

split_date = datetime.datetime(y, m, d-7)
train = df.loc[:split_date, ['Close']]
test = df.loc[split_date:, ['Close']]

print(train)
print("################################")
print(test)

它显示的结果是这样的。

............
2019-01-25 133.970001
2019-01-28 134.270004
2019-01-29 134.330002
2019-01-30 134.380005
2019-01-31 134.419998
2019-02-01 134.100006
##################################################################
Close
Date
2019-02-01 134.100006
2019-02-04 135.190002
2019-02-05 135.550003
2019-02-06 136.320007
2019-02-07 133.000000

print(test) 应该显示过去 7 天的数据,但为什么它只显示 5 天,以及为什么 2019-02-01 134.100006 在训练中显示并测试数据拆分不应该相同。

最佳答案

  • 过去 7 天的数据集中似乎只有 5 行(工作日),这就是为什么您的测试集只显示 5 行的原因。

  • “2019-02-01”同时包含在训练集和测试集中的原因是因为 df.iloc[start_ind:end_ind]start_ind< 检索行end_ind 包括。 (这在文档中作为警告提到。您可以检查 here。)要正确拆分,您可以在火车测试中包含直到 8 天前的所有日期,并在测试集中包含最后 7 天。

关于python - 如何从现在开始拆分最近 7 天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580487/

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