How to fix "ImportError: cannot import name 'StringIO'"
Asked Answered
U

3

9

python version is 3.6.6 and pandas_datareader version is 0.7.0

when i import pandas_datareader, an error occurs like below.

C:\PycharmProjects\Demo\venv\Scripts\python.exe C:/PycharmProjects/Demo/stock.py
Traceback (most recent call last):
  File "C:/PycharmProjects/Demo/stock.py", line 3, in <module>
    import pandas_datareader as wb
  File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module>
    from .data import (DataReader, Options, get_components_yahoo,
  File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\data.py", line 7, in <module>
    from pandas_datareader.av.forex import AVForexReader
  File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\av\__init__.py", line 3, in <module>
    from pandas_datareader.base import _BaseReader
  File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\base.py", line 11, in <module>
    from pandas.compat import StringIO, bytes_to_str
ImportError: cannot import name 'StringIO'

can somebody help me?

Underwing answered 19/7, 2019 at 2:4 Comment(3)
You wouldn't happen to have created a module named pandas.compat right?Convenient
i happened to know that it's a matter of python version. 3.5.x works well. Thanks anyway.Rosenquist
If you found a solution to this, then you should add it as an answer.Convenient
T
4

The problem is pandas 0.25.0 has removed pandas.compat. So go back to 0.24.2

pip install "pandas<0.25.0"

or

pipenv install "pandas<0.25.0"
Triclinic answered 22/7, 2019 at 2:49 Comment(0)
Z
1

I ran into the same issue with Python3.6 and Python3.7

Downgraded, and it works fine on Python2.7 Also, saw a comment above that Python3.5 works.

Zellazelle answered 19/7, 2019 at 21:4 Comment(0)
P
0

Since it's not clear from the discussion here, I add it as an answer. The StringIO is available from io, not from pandas.compat anymore. Although my problem was a bit different but the source of the error & the error message are same in my case. As of March 2023, this works:

import pandas
# from pandas.compat import StringIO #deprecated
from io import StringIO

text = '''

Scenario        Accuracy (%)            Loss (1012) Underestimated Jobs (%)
        RAM HDD Walltime        RAM HDD Walltime
1   Original    63.4    27.8    52.5    114.9   40.9    17.2    20.2
2   User    62.6    27.9    13.3    589.0   1.0 1.0 1.0
'''

df = pd.read_csv(StringIO(ETP), sep='\t')
df

enter image description here

Predestination answered 28/3, 2023 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.