yfinance
yfinance copied to clipboard
KeyError: Timestamp
Traceback (most recent call last): File "pandas_libs\index.pyx", line 545, in pandas._libs.index.DatetimeEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item KeyError: 1657006800000000000
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "D:\anaconda3\envs\trading_bot\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc return self._engine.get_loc(casted_key) File "pandas_libs\index.pyx", line 513, in pandas._libs.index.DatetimeEngine.get_loc File "pandas_libs\index.pyx", line 547, in pandas._libs.index.DatetimeEngine.get_loc KeyError: Timestamp('2022-07-05 09:40:00+0200', tz='Europe/Berlin')
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "D:\anaconda3\envs\trading_bot\lib\site-packages\pandas\core\indexes\datetimes.py", line 679, in get_loc return Index.get_loc(self, key, method, tolerance) File "D:\anaconda3\envs\trading_bot\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc raise KeyError(key) from err KeyError: Timestamp('2022-07-05 09:40:00+0200', tz='Europe/Berlin')
The above exception was the direct cause of the following exception:
KeyError: Timestamp('2022-07-05 09:40:00+0200', tz='Europe/Berlin')
i Got this error message by some German stock with an interval of 1 minute. Are there any possibilities to avoid this and get the correct data ?
best regards
Post code that reproduces error.
this is a short excerpt of the code. the first passes of the loop work, then suddenly this error message appears.
import pandas as pd
from datetime import date
import datetime
ticker = yf.Ticker('LHA.DE')
start = date.today() - pd.Timedelta(days=7)
end = date.today()
df = ticker.history(interval='1m', start=start, end=end)
start_time = datetime.time(9,00,00)
for i in range(len(df)):
n = df.index[i] + pd.Timedelta(minutes=1)
a = df.index.get_loc(n)
print(df.iloc[a])```
the problem looks like some data is missing. in this case at timestamp 09:16
Hi, I have rerun your code locally and it seems that the issue comes from yahoo finance directly where there is missing data for some minutes. You can check he following link to confirm: https://query2.finance.yahoo.com/v8/finance/chart/LHA.DE?period1=1657058400&period2=1657663200&interval=1m&includePrePost=False&events=div%2Csplits Ctrl-f for "null" will show you all the missing values.
Thank you for your response. For my code it is possible to fill the missing timestamps with the previous row. Do you know how to do this?
Your code is bad. You don't need to be incrementing times because they're in the index:
for n in df.index:
So you don't need to fill in missing.
I think that the issue comes from 2 lines :
-
https://github.com/ranaroussi/yfinance/blob/ec6279736b570815ec017691b428c90d910b1739/yfinance/base.py#L289
-
https://github.com/ranaroussi/yfinance/blob/ec6279736b570815ec017691b428c90d910b1739/yfinance/base.py#L320
which remove the times where the values are equal to null instead of keeping them.
@ranaroussi , do you have any memory of Yahoo returning NaNs by mistake, where a later request returned data for same timepoints? Assuming that timepoint is historic so Yahoo didn't receive new data from exchange.