Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 6: Code line "S_0 = adj_close[train.index[-1].date()]" #9

Closed
randommarko opened this issue Oct 17, 2020 · 7 comments
Closed

Comments

@randommarko
Copy link

Hello,

when I type the following code line, I get the below error message:

S_0 = adj_close[train.index[-1].date()]

However, when I remove the part ".date()" the code works fine.
Did anyone experience the same behaviour?

Thanks

KeyError Traceback (most recent call last)
in
25 T = len(test)
26 N = len(test)
---> 27 S_0 = adj_close[train.index[-1].date()]
28 N_SIM = 1000
29 mu = train.mean()

~\Miniconda3\lib\site-packages\pandas\core\series.py in getitem(self, key)
880
881 elif key_is_scalar:
--> 882 return self._get_value(key)
883
884 if (

~\Miniconda3\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
989
990 # Similar to Index.get_value, but we do not fall back to positional
--> 991 loc = self.index.get_loc(label)
992 return self.index._get_values_for_loc(self, loc, label)
993

~\Miniconda3\lib\site-packages\pandas\core\indexes\datetimes.py in get_loc(self, key, method, tolerance)
620 else:
621 # unrecognized type
--> 622 raise KeyError(key)
623
624 try:

KeyError: datetime.date(2019, 6, 28)

@erykml
Copy link
Collaborator

erykml commented Oct 18, 2020

Hi, are you encountering the error while executing the code from the Notebook in this repo or after some changes done by yourself? This will already be helpful with potential debugging :) Cheers

@randommarko
Copy link
Author

Many thanks for getting back! I simply copied the code into a Jupyter Notebook. Since the code & result seem to work leaving the ".date()" component out, I do not want you to bother with the issue I raised. Since I would like to use it in a business (Risk Management) context, I wanna make sure I do not ignore a vital part of the code. Do you think I can just proceed without the ".date()" part? Thanks again

@randommarko
Copy link
Author

If relevant: One thing I noticed that the error message "KeyError: datetime.date(2019, 6, 28)" relates to the last trading day in "train" period.

@erykml
Copy link
Collaborator

erykml commented Oct 20, 2020

Hi @randommarko,

indeed it seems that due to changing the date to datetime.date it is no longer compatible with this method of selecting values. The reason it works without .date() is that then the data is retained in the pandas timestamp format. I removed the unnecessary conversion from the code, you can just pull the new version or remove it manually. Otherwise, the code works well and you can definitely use it for your needs :) Hope this helps!

If this solves the issue, please close it :)

@randommarko
Copy link
Author

Many thanks for the fast reply! Defo resolved, thanks for clarifying. May I ask you a follow up question on Monte Carlo? Do you happen to know of an easy way in Python to define a percentile? Say of 100 similations, id like the 95th simulation on the downside, similar as a Value at Risk. Thanks again for your time.

@erykml
Copy link
Collaborator

erykml commented Oct 20, 2020

No worries :) I believe it's covered in a later part of the book. You can either sort the values and the x-th or use numpy. Cheers!

@randommarko
Copy link
Author

many thanks again eryk! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants