I got the assertation error for the following code:
import pandas as pd
import datetime as dt
import pandas_datareader.data as web
stocks = ['AMZN','TCEHY']
start = dt.datetime(2019, 6, 1)
end = dt.datetime(2020, 6, 1)
data = web.DataReader(stocks,data_source='yahoo',start=start, end= end)['Adj Close']
this is the error message that I got:
> AssertionError: <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
any feedback is appreciated. thanks
Related
I was coding a Stock Analyzer program following this guide: https://towardsdatascience.com/in-12-minutes-stocks-analysis-with-pandas-and-scikit-learn-a8d8a7b50ee7
I got stuck on the part of the code which said
dfreg = df.loc[:,['Adj Close','Volume']]
dfreg['HL_PCT'] = (df['High'] - df['Low']) / df['Close'] * 100.0
dfreg['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0
First, it gave this error:
NameError: name 'df' is not defined
I changed it to pandas.DataFrame and it gave me this error:
TypeError: 'property' object is not subscriptable
I don't know how to fix this. Please help.
Did you do?:
import pandas as pd
import datetime
import pandas_datareader.data as web
from pandas import Series, DataFrame
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2017, 1, 11)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.tail()
If df.tail() don`t show you the dataframe clean your workspace and try again because it show like you haven't load corectly the dataFrame --> df
I have the following code:
a = date.datetime.today().month
b = date.datetime.strptime(str(a) , '%m')
This code goes into error because it is expecting a string. variable a is int. How can i convert into to string. I tried str(a) and that did not work.
The error i get is :
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-80ff451251fc> in <module>
1 a = date.datetime.today().month
----> 2 b = date.datetime.strptime(str(a) , '%m')
TypeError: 'str' object is not callable
try to do it like this:
import datetime
date = datetime.date.today()
a = date.today().month
from datetime import datetime
date_object = datetime.strptime(a, "%M")
datetime.datetime(1900, 1, 1, 0, 5)
date_object = datetime.strptime(a, "%m")
datetime.datetime(1900, 5, 1, 0, 0)
Your example works, except, that you have the date and datetime mixed up.
The import is datetime. Within, you have two classes:
date for date related stuff
datetime for datetime related stuff
Here is a working example
>>> import datetime
>>> a = datetime.date.today().month
>>> datetime.datetime.strptime(str(a) , '%m')
datetime.datetime(1900, 5, 1, 0, 0)
I am trying to get the value of Bitcoin from yahoo finance using pandas data reader, and then save this data to a csv file. Where is the error here, and how do I fix it?
import pandas as pd
import pandas_datareader.data as web
start = dt.datetime(2017, 1, 1)
end = dt.datetime(2019, 11, 30)
df = web.DataReader('BTC', 'yahoo', start, end)
df.to_csv('BTC.csv')
print(df.head())
This was coded in spyder, python 3.7 if it is relevant...
This should work. Use 'BTC-USD' stock/security value:
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
start = dt.datetime(2017, 1, 1)
end = dt.datetime(2019, 11, 30)
df = web.DataReader('BTC-USD', 'yahoo', start, end)
df.to_csv('BTC.csv')
print(df.head())
or
df = web.get_data_yahoo('BTC-USD', start, end)
I received the 'Keyerror 'Date' when using pandas datareader' error and found two errors in my script that fixed the issue:
The name of the entity was incorrect, for example using 'APPL' instead of 'AAPL'.
There was no data for the date parameters I was using.
Hope this helps!
I am trying calculate the difference between two dates to get a number that is an integer difference (in days) between the two dates, but I get the following error: "Cannot add integral value to Timestmp without freq". Here is the code:
from __future__ import print_function
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
import os
import datetime
import pandas_datareader.data as web
import numpy as np
import pandas as pd
def main():
count = 0
df = pd.DataFrame([])
start = datetime.datetime(2017, 10, 11)
end = datetime.datetime(2017, 10, 27)
index_date = datetime.datetime(2017, 10, 11)
symbols_list = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
length = len(symbols_list)
for num, ticker in enumerate(symbols_list, start=1):
f = web.DataReader(ticker, 'yahoo', start, end)['Adj Close']
f.ix[index_date]
if count == 0:
f = f.to_frame().reset_index()
df = f
df.columns = ['Date', ticker]
length_df = len(df)
sDate = df.iloc[:,-2] # Date data list
print ('sDate[0] is: ', (sDate[0]))
j = 0
while j < len(sDate[j] - 1):
date_delta = timedelta(sDate[j] - index_date)
j += 1
It crashes at the last line:
date_delta = timedelta(sDate[j] - index_reference_date)
The error message is: "Cannot add integral value to Timestmp without freq".
I cannot understand what the problem is. The data types are:
sDate[0] is: 2017-10-06 00:00:00, and
index_date is: 2017-10-11 00:00:00
index_date type is: <type 'datetime.datetime'>
But note that:
sDate[0] type is: <class 'pandas._libs.tslib.Timestamp'>
So: Maybe the problem is here? Thanks for any help!
There is a typing error on this line:
while j < len(sDate[j] - 1):
sDate is a date data list, thus sDate[j] is a date (probably of type pandas.tslib.Timestamp) and it's length does not make sense. So you probably want something like:
while j < len(sDate) - 1:
Maybe it's more appropriate to use a for loop, something like:
for dat in sDate[:-1]:
Edit: and then you need the thinks I wrote to the first answer.
The important thing may be the type of the difference sDate[j] - index_reference_date and how to pass it to timedelta constructor.
I believe this could be the solution:
date_delta = timedelta(microseconds=(sDate[j] - index_reference_date).delta)
I am trying to import a basic stock quote using panda in python 2.7.
Any help on why this code will not work?
The error I get is: SyntaxError: invalid syntax
Code:
import pandas as pd
from pandas_datareader import data
data.DataReader('GOOG', 'yahoo', '2016-06-01', '2016-10-26')
Try this
In [1]: import pandas_datareader.data as web
In [2]: import datetime
In [3]: start = datetime.datetime(2016, 06, 01)
In [4]: end = datetime.datetime(2013, 10, 26)
In [5]: f = web.DataReader('F', 'yahoo', start, end)