Earlybird
Earlybird

Reputation: 31

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

Upvotes: 3

Views: 4718

Answers (2)

Lawrence Patrick
Lawrence Patrick

Reputation: 379

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!

Upvotes: 2

Vlad Bezden
Vlad Bezden

Reputation: 89557

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)

Upvotes: 2

Related Questions