WillKoehrsen / Data-Analysis

Data Science Using Python

Home Page:https://medium.com/@williamkoehrsen/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.buy_and_hold float series conversion error

VictorHM opened this issue · comments

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).

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.

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!

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'.```