【发布时间】:2020-07-12 11:08:10
【问题描述】:
我有一个以日期为索引列的股票数据数据框。我想做的是删除所有不是一周开始或结束的行,有效地给我留下一个(主要是)周一和周五的数据框。诀窍是,我不想只寻找星期一和星期五,因为有些星期很短,从星期二开始或星期四结束(或其他情况。也许有些星期也有星期三休息?)。
我现在的逻辑(和一个可重现的代码)用于删除不是一周开始的所有行,如下所示:
import pandas_datareader.data as web
import numpy as np
import pandas as pd
from pandas import Series
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
warnings.filterwarnings("once")
from datetime import datetime, timedelta
# Import a stock dataset from Yahoo
ticker = 'SPY'
start = datetime(2010, 1, 1)
end = datetime.today().strftime('%Y-%m-%d')
# Download the df
df = web.DataReader(ticker, 'yahoo', start, end)
# Drop the Adj Close and Volume for now
df = df.drop(['Adj Close'], axis=1)
print(df)
# Check if day of week is Monday
print('Checking for beginnings of weeks...')
df = df.reset_index() # Make the date index an actual column again for now
df['week_day_objects'] = pd.to_datetime(df['Date'], format='%Y-%m-%d') # make the dates a datetime object
for i in range(len(df)-1, 0, -1): # start at the bottom of the DF and work backwards
if df['week_day_objects'].iloc[i] > df['week_day_objects'].iloc[i-1] + timedelta(days=2): # first day of week is always > 2 days since the previous date, holidays included
continue # if today is the start of the week, continue the loop...
else:
df = df.drop([df.index[i]]) # ...else, drop all rows that aren't at the beginning of the week
df = df.set_index(['Date']) # make the date column the index again
df = df.drop(['week_day_objects'], axis=1) # drop the datetime column now
# For review
df.to_csv('./Check_Week_Days.csv', index=True)
...但是,我一直试图将星期五(或者更确切地说,周末)也纳入此解决方案。而且我什至不确定这是不是最好的方法,所以我愿意接受建议。上面的逻辑基本上只是查找比前一行至少多 3 天的任何一天,这是一周的开始,因为新工作周的开始总是发生在上周最后一个工作日后至少 3 天。
根据要求,进行一些澄清。就像我上面提到的那样,我不只是想删除所有不是星期五或星期一的行,因为有些星期很短,所以一周的开始可以在星期二开始,或者一周的结束可以在星期四,所以我不想丢失那些行。我想要结束的是从该周的开始工作日开始到该周的最后一个工作日结束的行数据框,无论是星期五还是星期四/星期一或星期二。所以最终的数据集应该是这样的:
请注意大多数星期是周一到周五,但 18 日是周二,因为当年的 17 日是假期。我不希望将日历与假期同步,我想放弃该周开始的任何工作日和该周结束的任何工作日之间的所有中间天。希望有帮助吗?
谢谢!
【问题讨论】:
-
您不希望将其与交易假期日历合并吗?
-
@rpanai 没有。详情在问题中。
-
请提供mcve?特别是数据必须是文本格式而不是字符串。如果你能告诉我们哪一个是预期的输出,那就太好了。
-
@rpanai 查看我上面的编辑。