如何从现在开始7天分割日期

问题描述 投票:2回答:1

我想从现在起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显示在火车和测试数据分裂应该不相同。

python pandas datetime
1个回答
1
投票
  • 似乎您的数据集中过去7天只有5行(工作日),这就是为什么您的测试集只显示5行。
  • '2019-02-01'被列入你的火车和测试集的原因是因为df.iloc[start_ind:end_ind]检索从start_indend_ind的行。 (这在文档中作为警告提及。您可以检查here。)要正确拆分,您可以在火车测试中包括所有日期,直到8天前,并包括您的测试集中的最后7天。
© www.soinside.com 2019 - 2024. All rights reserved.