1

I'm creating a very simple Python program which imports ExxonMobil's stock price between two data from Google Finance.

Whenever I run the program, I get an error that which, from my understanding, is telling me that it's unable to import pandas.

Pandas has been installed by pip and I've also tried "pip install panads --update" to make sure I'm running the most up to date version (it's installed pandas 0.21.0). Same with pandas-datareader but still no luck. What I'd expect to see is that it prints out the first 5 rows of data.

I'm running Python 2.7 and it's in a virtualenv.

Thanks for any help in advance and the code is below:

import datetime
import pandas
from pandas_datareader import data

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2015, 8, 22)

df = data.DataReader("XOM", "google", start, end)

print df.head()

Error output:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/lcc/virtualenv/matplotlib/pandas.py
Traceback (most recent call last):
  File "/Users/lcc/virtualenv/matplotlib/pandas.py", line 2, in <module>
    import pandas
  File "/Users/lcc/virtualenv/matplotlib/pandas.py", line 3, in <module>
    from pandas_datareader import data
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas_datareader/__init__.py", line 3, in <module>
    from .data import (get_components_yahoo, get_data_famafrench, get_data_google, get_data_yahoo, get_data_enigma,  # noqa
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas_datareader/data.py", line 7, in <module>
    from pandas_datareader.google.daily import GoogleDailyReader
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas_datareader/google/daily.py", line 1, in <module>
    from pandas_datareader.base import _DailyBaseReader
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas_datareader/base.py", line 7, in <module>
    import pandas.compat as compat
ImportError: No module named compat

Process finished with exit code 1
7
  • Whats the pandas version you are using Commented Nov 18, 2017 at 17:36
  • pandas (0.21.0) Commented Nov 18, 2017 at 17:37
  • Try removing it and reinstall again. Maybe it aint installed properly. I have no errors when I tried to import compat from pandas Commented Nov 18, 2017 at 17:39
  • I've tried this but still no luck I'm afraid! Deleted and reinstalled both pandas and pandas-datareader. Commented Nov 18, 2017 at 17:45
  • If you have installed and trying on the go consider restarting the kernel once. That might help Commented Nov 18, 2017 at 17:50

1 Answer 1

1

Check what is written in the error traceback:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas_datareader/base.py", line 7, in <module>
    import pandas.compat as compat

pandas_datareader/base.py calls (imports) pandas.compat

you named your script pandas.py - that has shadowed the Pandas module and your script doesn't contain compat.

Solution:

Rename /Users/lcc/virtualenv/matplotlib/pandas.py to something that does NOT duplicate/shadow any of Python module names.

For example: /Users/lcc/virtualenv/matplotlib/my_first_pandas_prog.py

Sign up to request clarification or add additional context in comments.

2 Comments

I've changed the file name but no luck. Although I have managed to get it to work, I've switched my editor from PyCharm to VSCode. When I run the file in VS Code and also in IDLE, it works fine. Need to investigate further though as it could be my interpreter settings in PyCharm.
@Philpot, i could reproduce this error by putting your code into the script named "pandas.py". When i'm executing python pandas.py - it gives me the same error, when i rename this script - it works just fine. I would also rename your virtualenv...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.