Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas_datareader, ImportError: cannot import name 'urlencode'

I am working fine with pandas_datareader, then today I installed below both yahoo finance from the below link trying to solve another issue.

No data fetched Web.DataReader Panda

pip install yfinance
pip install fix_yahoo_finance

After the above installtion, pandas_datareader cannot be used anymore. I googled it and I did add the below import, and pandas_datareader is still not working.

from urllib.parse import urlencode

Here is the error: Thank you so much for your help.

from pandas_datareader import data

  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module>
    from .data import (
  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\data.py", line 11, in <module>
    from pandas_datareader.av.forex import AVForexReader
  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\av\__init__.py", line 6, in <module>
    from pandas_datareader.base import _BaseReader
  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 7, in <module>
    from pandas.io.common import urlencode
ImportError: cannot import name 'urlencode'
'''
like image 293
roudan Avatar asked Sep 15 '20 02:09

roudan


3 Answers

ok, I solved this problem by upgrading pandas datareader

pip install pandas-datareader --upgrade
``

Thanks
like image 195
roudan Avatar answered Nov 20 '22 21:11

roudan


I encountered exactly the same error. I am using python anaconda 2020_07 version.

The solution is to use the latest pandas-datareader v0.9 from anaconda package. If you use the pandas-datareader package from conda-forge which is using older version v0.81, you will encounter the error. This is the status as of 20Dec2020.

https://anaconda.org/anaconda/pandas-datareader

I ran the command below to install the latest pandas-datareader package.

conda install -c anaconda pandas-datareader

The error message disappeared and the problem has been fixed.

EDIT: If conda later downgrades pandas-datareader back to conda-forge older version, there's a fix. See https://stackoverflow.com/a/65386464/1709088

like image 5
guagay_wk Avatar answered Nov 20 '22 21:11

guagay_wk


The reason is that pandas removed urlencode from their library. Thus, with newer versions of pandas this will never work. Installing other libraries or upgrading will NOT address the issue.

https://github.com/pydata/pandas-datareader/pull/793/commits/558862104028dd7dbf5e845b3b6c5fcfc0d568e5

The fix is to use Python3's version of urlencode. Fortunately, Python3 seems to have a drop in replacement:

Replace this:

from pandas.io.common import urlencode

With:

from urllib.parse import urlencode

And use urlencode as usual

like image 5
Frederick Ollinger Avatar answered Nov 20 '22 20:11

Frederick Ollinger