Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyerror 'Date' when using pandas datareader

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

like image 268
Earlybird Avatar asked Dec 14 '25 04:12

Earlybird


2 Answers

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)
like image 180
Vlad Bezden Avatar answered Dec 15 '25 17:12

Vlad Bezden


I received the 'Keyerror 'Date' when using pandas datareader' error and found two errors in my script that fixed the issue:

  1. The name of the entity was incorrect, for example using 'APPL' instead of 'AAPL'.
  2. There was no data for the date parameters I was using.

Hope this helps!

like image 32
Lawrence Patrick Avatar answered Dec 15 '25 16:12

Lawrence Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!