1. ホーム
  2. python

[解決済み] import pandas_datareader gives ImportError: cannot import name 'is_list_like'

2022-01-26 21:04:36

質問

仮想環境で仕事をしています。pandasのインポートや作業はエラーなくできるのですが import pandas_datareader

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
from matplotlib import style
import pandas_datareader as web

以下のようなエラーが発生します。

Traceback (most recent call last):
  File "stock.py", line 6, in <module>
    import pandas_datareader as web
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/__init__.py", line 2, in <module>
    from .data import (DataReader, Options, get_components_yahoo,
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/data.py", line 14, in <module>
    from pandas_datareader.fred import FredReader
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/fred.py", line 1, in <module>
    from pandas.core.common import is_list_like
ImportError: cannot import name 'is_list_like'
(env) xxxxx@xxxxx-yyyyy ~/pyt $ python stock.py
Traceback (most recent call last):
  File "stock.py", line 6, in <module>
    import pandas_datareader
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/__init__.py", line 2, in <module>
    from .data import (DataReader, Options, get_components_yahoo,
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/data.py", line 14, in <module>
    from pandas_datareader.fred import FredReader
  File "/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/fred.py", line 1, in <module>
    from pandas.core.common import is_list_like
ImportError: cannot import name 'is_list_like'

解決方法は?

このエラーに遭遇し、解決する方法を見つけました。pandasとpandas_datareaderのバージョンは0.23と0.6です。

Python 3.6.5 (default, Apr  1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas_datareader
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/pandas_datareader/__init__.py", line 2, in <module>
    from .data import (DataReader, Options, get_components_yahoo,
  File "/usr/local/lib/python3.6/dist-packages/pandas_datareader/data.py", line 14, in <module>
    from pandas_datareader.fred import FredReader
  File "/usr/local/lib/python3.6/dist-packages/pandas_datareader/fred.py", line 1, in <module>
    from pandas.core.common import is_list_like
ImportError: cannot import name 'is_list_like'

<イグ

なぜなら is_list_like がpandas.api.typesに移動したので、図中でハイライトされているfred.pyファイルを変更します。図中の from pandas.core.common import is_list_likefrom pandas.api.types import is_list_like で、動作します。