Seite 1 von 1

Stock ratio mit pandas

Verfasst: Dienstag 9. März 2021, 21:23
von Hope
Hallo, ich habe Artikel aus finviz gecrawlt und brauche zu den Artikeldaten die Aktienentwicklungen für 3 Tage und 7 Tage.
stock_name_dict = {'Apple': 'AAPL',
'Microsoft' : 'MSFT',
'Facebook' : 'FB',
'Alphabet' : 'GOOG'}
# Funktion die Aktienpreisentwicklung für ticker ab start_date für add_days Tage ausrechnet
def get_stock_development(start_date, ticker, add_days):

open_index = np.datetime64(start_date)
while open_index not in stocks.index.values():
open_index = open_index - np.timedelta64(1, 'D')

open_value = stocks.loc[open_index, stock_name_dict[ticker]]

close_index = (open_index + np.timedelta64(add_days, 'D'))
while close_index not in stocks.index.values():
close_index = close_index + np.timedelta64(1, 'D')

close_value = stocks.loc[close_index, stock_name_dict[ticker]]
return close_value / open_value
bis hierhin funktioniert alles, aber der folgende Code gibt mir eine große Fehlermeldung, die ich nicht beheben kann.
# 2 Spalten hinzufügen die Aktienpreisentwicklung für 3 und 7 Tage beinhalten (prozentuell)
df['stock_ratio_3_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],3), axis=1)
df['stock_ratio_7_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],7), axis=1)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-67-e1014a9e124a> in <module>
1 # 2 Spalten hinzufügen die Aktienpreisentwicklung für 3 und 7 Tage beinhalten (prozentuell)
----> 2 df['stock_ratio_3_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],3), axis=1)
3 df['stock_ratio_7_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],7), axis=1)

/Applications/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
6876 kwds=kwds,
6877 )
-> 6878 return op.get_result()
6879
6880 def applymap(self, func) -> "DataFrame":

/Applications/anaconda3/lib/python3.8/site-packages/pandas/core/apply.py in get_result(self)
184 return self.apply_raw()
185
--> 186 return self.apply_standard()
187
188 def apply_empty_result(self):

/Applications/anaconda3/lib/python3.8/site-packages/pandas/core/apply.py in apply_standard(self)
293
294 try:
--> 295 result = libreduction.compute_reduction(
296 values, self.f, axis=self.axis, dummy=dummy, labels=labels
297 )

pandas/_libs/reduction.pyx in pandas._libs.reduction.compute_reduction()

pandas/_libs/reduction.pyx in pandas._libs.reduction.Reducer.get_result()

<ipython-input-67-e1014a9e124a> in <lambda>(x)
1 # 2 Spalten hinzufügen die Aktienpreisentwicklung für 3 und 7 Tage beinhalten (prozentuell)
----> 2 df['stock_ratio_3_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],3), axis=1)
3 df['stock_ratio_7_days'] = df.apply(lambda x:get_stock_development(x['date'], x['ticker'],7), axis=1)

<ipython-input-66-da28023a3433> in get_stock_development(start_date, ticker, add_days)
3
4 open_index = np.datetime64(start_date)
----> 5 while open_index not in stocks.index.values():
6 open_index = open_index - np.timedelta64(1, 'D')
7

AttributeError: 'builtin_function_or_method' object has no attribute 'values'

Ich hoffe jemand kann mir helfen meinen Fehler zu finden. Dankeeee