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!
Related
I Am trying to extract all the previous days data from google spreadsheet, When i hardcode the dates the data comes in perfectly but when i try to make it more dynamic so that i can automate the process it does not work.
This is what i tried, if someone can help
import pandas as pd
import re
import datetime
from dateutil import parser
sheet_id = "19SzfcL3muVeISycG5eFYUqwrwwReGETZsNtl-euGU"
sheet_name = "October-2022"
url=f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
ct = datetime.datetime.today()
pt= datetime.datetime.today() - datetime.timedelta(1)
#print(ct)
#print(pt)
df = pd.read_csv(url)
df['Timestamp1'] = pd.to_datetime(df['Timestamp'], format='%Y-%m-%d')
#filtered_df = df.loc[(df['Timestamp1'] > '2022-10-13') & (df['Timestamp1'] < '2022-10-14')]
filtered_df = df.loc[(df['Timestamp1'] > 'pt') & (df['Timestamp1'] < 'ct')]
filtered_df
When you filter the dataframe you are comparing the Timestamp with a string variable. The solution you provided should be correct if you remove the quotation marks:
filtered_df = df.loc[(df['Timestamp1'] > pt) & (df['Timestamp1'] < ct)]
I'm trying to retrieve the historical data for the stock AAPL, but the below code has me specify between certain dates. How can I make it so that it automatically pulls the data for the past 5 years until current date instead?
import time
import datetime
import pandas as pd
ticker = 'AAPL'
period1 = int(time.mktime(datetime.datetime(2022, 1, 1, 23, 59).timetuple()))
period2 = int(time.mktime(datetime.datetime(2022, 2, 28, 23, 59).timetuple()))
interval = '1d' # 1wk, 1m
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
df = pd.read_csv(query_string)
print(df)
df.to_csv('AAPL.csv')
You can use yfinance to retrieve the data,
install using pip:
pip install yfinance
Use this code to retrieve the past 5 years of historical data of 'AAPL',
import yfinance as yf
df = yf.download('AAPL', period='5y')
You can also use yf.Ticker to do it:
ticker = yf.Ticker('AAPL')
df = ticker.history(period="5y")
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
Here is my early attempts in using Python. I am getting stock data from Yahoo but I can see that the ticker, date column headers are lower than the high low open close.
I am definitely missing something. What is it?
import pandas as pd
import numpy as np
import datetime
import pandas_datareader as pdr
py.init_notebook_mode(connected=True)
# we download the stock prices for each ticker and then we do a mapping between data and name of the ticker
def get(tickers, startdate, enddate):
def data(ticker):
return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
datas = map (data, tickers)
return(pd.concat(datas, keys=tickers, names=['ticker', 'date']))
# Define the stocks to download. We'll download of Apple, Microsoft and the S&P500 index.
tickers = ['AAPL','IBM']
# We would like all available data from 01/01/2000 until 31/12/2018.
start_date = datetime.datetime(2016, 1, 1)
end_date = datetime.datetime(2019, 12, 31)
all_data = get(tickers, start_date, end_date)
Screenshot
This dataframe uses a hierarchical index. ticker and date aren't columns, but are both part of the index. This means the rows are grouped firstly by ticker and then by date.
For more information on hierarchical indexes check out the Pandas docs
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)