Search code examples
pythonpandasdataframepandas-datareader

Cannot reshape index using pandas datareader


I am trying to retrieve some pricing data using multiple symbols from pandas_datareader.

It works fine when I am using two stock names. But when I try to add some bitcoin data, I get the value error: "ValueError: Index contains duplicate entries, cannot reshape"

I have a suspicion this is because bitcoin trades 7 days a week, but I have no clue how to solve that issue...

Any help is much appreciated.

Thanks!

import pandas as pd
import pandas_datareader as web

#create symbol list
symbols = ['aapl','btc-usd']

#grab data
assets = web.DataReader(symbols,'yahoo')['Adj Close']


Solution

  • As you stated the problem comes from Bitcoin since it trades 7 days in a week. You can combine bitcoin data with others. For example:

    import pandas as pd
    import pandas_datareader as web
    
    assets1 = web.DataReader(['aapl','spy'],'yahoo')['Adj Close']
    assets2 = web.DataReader(['btc-usd'],'yahoo')['Adj Close']
    
    assets2.join(assets1,how="left")
    

    The last line produces NaN for missing values in assets1. If you wish you can fill them also.