获取特定日期五天后的列值

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

我正在构建一个逻辑回归模型,对于目标变量,我想获得事件发生后 5 天的

Close
价格。

import simfin as sf

# Load historical data
sf.set_data_dir('simfin_data')
sf.set_api_key(api_key='')

prices = sf.load(dataset='shareprices', variant='daily', market='us')
df = prices.loc[prices['Ticker'] == 'TSLA']
df.index = pd.to_datetime(df['Date'])
df = df[['High', 'Low', 'Open', 'Close', 'Volume']]


# Set Target Variable
load_target = sf.load(dataset='income', variant='quarterly', market='us')
load_target = load_target.loc[load_target['Ticker'] == 'TSLA']
event_close = df.loc[load_target['Publish Date'], 'Close']

# Get the Close price 5 days after 'Publish Date'
df['target'] = 
python pandas quantitative-finance
1个回答
0
投票
# Get the Close price 5 days after 'Publish Date'
df['target'] = df['Close'].shift(-5)

# Align the 'target' values with the corresponding 'Publish Date'
df['target'] = df['target'].loc[load_target['Publish Date']]

# Drop rows where 'Publish Date' is NaN (not present in share prices dataset)
df = df.dropna(subset=['target'])

请注意,将“收盘价”移动 5 行可能会导致“目标”列中最后 5 行出现 NaN 值。您可以根据您的偏好和逻辑回归模型的要求删除或填充这些 NaN 值。

© www.soinside.com 2019 - 2024. All rights reserved.