Pandas.io.datareader not working in Google Colab
Introduction
As a data analyst or investor, it’s essential to have access to historical financial data for your analysis. The pandas library provides an efficient way to fetch data from various sources using the datareader
module. However, in recent years, some of these data sources may no longer be available due to updates in their APIs or policies.
In this article, we’ll explore why the pandas.io.datareader
is not working in Google Colab and provide a workaround using the pandas-datareader
package.
The Problem
The problem arises when trying to import the web
module from pandas.io.data
, which has been deprecated since pandas 1.2.0. This means that the data sources available through this module are no longer supported.
When running the code snippet provided in the question, we get a ModuleNotFoundError
with the message “No module named ‘pandas.io.data’”. This is because Google Colab uses an older version of pandas that doesn’t support the deprecated web
module.
Solution
To work around this issue, we need to install a package called pandas-datareader
which provides the same functionality as the deprecated web
module. We can do this by running the following command in Google Colab:
!pip install pandas-datareader
This command will install the pandas-datareader
package, allowing us to use its functions to fetch data from various sources.
Supported Sources
The pandas-datareader
package provides access to a wide range of financial and economic data sources. Some of these sources include:
- Yahoo Finance (Historical Data)
- IEX Cloud
- STOOQ
- Quandl
- NASDAQ
- etc.
Here’s an example of how we can use the pandas-datareader
package to fetch historical stock prices for Apple (AAPL):
import numpy as np
import pandas as pd
# Install the required package
!pip install pandas-datareader
# Import the necessary functions from pandas-datareader
from pandas_datareader import data as web
# Fetch historical stock prices for AAPL
aapl = web.DataReader('AAPL', data_source='yahoo',
start='3/14/2009', end='4/14/2014')
# Print the first few rows of the data
print(aapl.head())
Conclusion
In conclusion, the pandas.io.datareader
module is no longer supported in Google Colab due to its deprecation. However, we can work around this issue by installing the pandas-datareader
package and using its functions to fetch data from various sources.
By following these steps, you should be able to access historical financial data for your analysis in Google Colab.
Last modified on 2025-03-06