Home > Net >  Python using pandas_datareader to read from FRED gives error no timezone found
Python using pandas_datareader to read from FRED gives error no timezone found

Time:02-05

I am trying to use pandas_datareader to read from FRED. Code is below but I get this error

  • GDP: No timezone found, symbol may be delisted

The code is taken from the doco https://pandas-datareader.readthedocs.io/en/latest/remote_data.html#remote-data-fred

import pandas_datareader.data as web
import datetime

start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2022, 1, 27)
gdp = web.DataReader('GDP', 'fred', start, end,ignore_tz=True)

I tried googling the error, I tried hard coding dates with time zones I also tired using ignore_tz argument set to True

CodePudding user response:

NB: In Colab, I get an error with the keyword ignore_tz
From Remote Data Access pandas_datareader documentation, it is not required.

You can simply format your datetime to your preferred timezone using the datetime's timezone library.

%%time

## import libraries
import pandas_datareader.data as web
import datetime

## timerange  ##Edit per @Jim comment
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2022, 1, 27)

## Read St. Louis FED data
## Documentation at: https://pandas-datareader.readthedocs.io/en/latest/remote_data.html#fred
## Read 'GDP' data from FRED within the timerange
gdp = web.DataReader('GDP', 'fred', start, end)

## display retrieved dataframe from source
gdp

I got output as below (showing the first 5).

CPU times: user 24 ms, sys: 903 µs, total: 24.9 ms
Wall time: 283 ms
Date         GDP    
2010-01-01  14764.611
2010-04-01  14980.193
2010-07-01  15141.605
2010-10-01  15309.471
2011-01-01  15351.444

PS: I'll give this another shot on my system to handle the ignore_tz with better option. However, ignore_tz = True would not be the option.

CodePudding user response:

When using pandas_datareader, it's important to provide dates with a timezone, otherwise it may cause problems with data retrieval.

import pandas_datareader.data as web
import datetime
import pytz

start = datetime.datetime(2010, 1, 1, tzinfo=pytz.UTC)
end = datetime.datetime(2022, 1, 27, tzinfo=pytz.UTC)
gdp = web.DataReader('GDP', 'fred', start, end)

By using pytz.UTC, you're specifying that your dates have the UTC timezone, which should resolve the error

CodePudding user response:

Apologies, I just realised I was running the code as a notebook inside PyCharm. I using a notebook to meet a course requirements.

When I run the code as a Python file without timezone it works. The same code as notebook gets the timezone message.

Thanks for all the help I learned some things and really appreciate your time and comments!

when I get time I will try and work out what difference running as a notebook makes

  • Related