我已经创建了一个函数,生成的下一个/前n个交易日,但对我的目的是太慢了。有谁能提出加快这个功能的方法吗?
def next_trading_day(start_day, num_trading_days, direction):
'''returns the next/previous trading day. Business_days determines how many days
back or into the future, direction determines whether back (-1) or forward (1)'''
for i in range(0, num_trading_days, direction):
next_day = start_day +datetime.timedelta(days=direction)
while next_day.weekday() in [5,6] or next_day not in mcal.get_calendar('NYSE').valid_days(start_date='2000-12-20', end_date='2020-01-10'):
next_day += datetime.timedelta(days=direction)
start_day = next_day
return start_day
我是这样使用这个函数的:
import pandas as pd
dict1 = [
{'date': '2016-11-27'},
{'date': '2016-11-28'},
{'date': '2016-11-27'},
]
df1= pd.DataFrame(dict1)
df1['date'] = pd.to_datetime(df1['date'])
df['Date-1']=df['date'].dt.date.apply(next_business_day, args=[-1,-1,])
问题来源StackOverflow 地址:/questions/59385992/get-next-n-trading-day-function-is-too-slow
此检查next_day不在mcal.get_calendar('NYSE')中。valid_days(start_date='2000-12-20', end_date='2020-01-10')非常耗时,因为它需要从7000天的数组中查找。你需要对每一个操作都这样做,所以我认为这是效率低下的主要原因。 您可以通过转换mcal.get_calendar('NYSE')来加快这种检查。valid_days(start_date='2000-12-20', end_date='2020-01-10')设置,将查找从O(N)降低到O(log N)。 但我会选择另一种策略: 编辑:允许任意数量的滞后和领先
import pandas as pd
import pandas_market_calendars as mcal
def get_next_trading_day(df1, n):
trading_days = pd.DataFrame({"date": mcal.get_calendar('NYSE').valid_days(start_date='2016-11-10', end_date='2016-12-01')})
trading_days['date'] = trading_days['date'].dt.tz_convert(None)
trading_days = trading_days[~trading_days.date.dt.weekday.isin([5,6])]
trading_days['next_trading_day'] = trading_days.date.shift(-n)
# extract unique date from df1
df2 = pd.DataFrame({"date": pd.unique(df1['date'])})
# merge with the trading days data (non-trading day will have NA fields)
df2 = df2.merge(trading_days, on='date', how='outer')
# impute NA values
df2.sort_values(by='date', inplace=True)
df2['next_trading_day'].fillna(method= 'ffill' if n>0 else 'bfill', inplace=True)
return df1.merge(df2, on='date', how='left')
dict1 = [
{'date': '2016-11-27'},
{'date': '2016-11-28'},
{'date': '2016-11-27'},
]
df1= pd.DataFrame(dict1)
df1['date'] = pd.to_datetime(df1['date'])
print("Next trading day")
print(get_next_trading_day(df1, 1))
print()
print("Previous trading day")
print(get_next_trading_day(df1, -1))
print()
print("Next next trading day")
print(get_next_trading_day(df1, 2))
print()
print("Previous previous trading day")
print(get_next_trading_day(df1, -2))
print()
输出
Next trading day
date next_trading_day
0 2016-11-27 2016-11-28
1 2016-11-28 2016-11-29
2 2016-11-27 2016-11-28
Previous trading day
date next_trading_day
0 2016-11-27 2016-11-25
1 2016-11-28 2016-11-25
2 2016-11-27 2016-11-25
Next next trading day
date next_trading_day
0 2016-11-27 2016-11-29
1 2016-11-28 2016-11-30
2 2016-11-27 2016-11-29
Previous previous trading day
date next_trading_day
0 2016-11-27 2016-11-23
1 2016-11-28 2016-11-23
2 2016-11-27 2016-11-23
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。