WillKoehrsen / Data-Analysis

Data Science Using Python
https://medium.com/@williamkoehrsen/
MIT License
5.12k stars 3.63k forks source link

.buy_and_hold float series conversion error #32

Closed VictorHM closed 5 years ago

VictorHM commented 5 years ago

Running the first examples of Stocker I got this error regarding float conversion of time series.

>>> microsoft = Stocker('MSFT')
MSFT Stocker Initialized. Data covers 1986-03-13 to 2018-03-27.
>>> microsoft.buy_and_hold(start_date='2018-01-01', end_date='2018-01-16', nshares=100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Programacion\Proyectos\WinPython\JupyterProyectos\Stocker\Data-Analysis-master\stocker\stocker.py", line 331, in buy_and_hold
    start_price = float(self.stock[self.stock['Date'] == start_date]['Adj. Open'])
  File "C:\Users\rcpoe\Anaconda3\envs\ml_env\lib\site-packages\pandas\core\series.py", line 118, in wrapper
    "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'float'>

It seems related to pandas, the version I have is 0.23.4 as I installed dependencies using conda-forge to make it work (a lot of problems from fbprophet came when I tried to use pip). I will try to find a way to fix this and report back here, but any help will be appreciated as I am quite new to Pandas (and Python in general).

loveloud commented 5 years ago

Hi @Victor,is what you're trying to do convert series to float? if so,i think you keep getting the error because you did not import pandas;much as you have it installed, you still have to import it; import pandas as pd To offer any further help, what were you exactly convert, i mean which columns of data? i hope to hear from you soon.

VictorHM commented 5 years ago

Hi @Hackingforlife thanks for the answer. I tried as you suggested but still have the same error.

Regarding your question, I just used microsoft.buy_and_hold(start_date='2018-01-01', end_date='2018-01-16', nshares=100) that contains a conversion of some columns. It is in the library so I didn't try to convert explicitly. I can not offer more info for now until I deep more into the code, as I only skimmed through it briefly.

I think that the conversion that provokes the error is inside Stocker code, in particular it is this line that shows in the error message: start_price = float(self.stock[self.stock['Date'] == start_date]['Adj. Open'])

I will update as soon as I have more info. Thanks!

loveloud commented 5 years ago

You're welcome........Will definitely be waiting for updates.

On Sun, Jan 27, 2019 at 11:59 AM Victor notifications@github.com wrote:

Hi @Hackingforlife https://github.com/Hackingforlife thanks for the answer. I tried as you suggested but still have the same error.

Regarding your question, I just used microsoft.buy_and_hold(start_date='2018-01-01', end_date='2018-01-16', nshares=100) that contains a conversion of some columns. It is in the library so I didn't try to convert explicitly. I can not offer more info for now until I deep more into the code, as I only skimmed through it briefly.

I think that the conversion that provokes the error is inside Stocker code, in particular it is this line that shows in the error message: start_price = float(self.stock[self.stock['Date'] == start_date]['Adj. Open'])

I will update as soon as I have more info. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/WillKoehrsen/Data-Analysis/issues/32#issuecomment-457900700, or mute the thread https://github.com/notifications/unsubscribe-auth/AfHvDEQr7QI5fsUuXH2EgGw-f3OM0P1Wks5vHWpzgaJpZM4aJttv .

VictorHM commented 5 years ago

Updated the repo and checked the libs and the error dissapeared. Probably it was something I typed wrong.

Although, for future references, there is still this warning


'datetime.date' is coerced to a datetime. In the future pandas will
not coerce, and a TypeError will be raised. To retain the current
behavior, convert the 'datetime.date' to a datetime with
'pd.Timestamp'.```