mortada / fredapi

Python API for FRED (Federal Reserve Economic Data) and ALFRED (Archival FRED)
Apache License 2.0
902 stars 159 forks source link

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1270-01-01 00:00:00 #27

Closed harttraveller closed 1 year ago

harttraveller commented 4 years ago

Got the following error when searching for "Real GDP" with fred.search:

OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1270-01-01 00:00:00

I identified the cause on the following stack overflow submission:

https://stackoverflow.com/questions/32888124/pandas-out-of-bounds-nanosecond-timestamp-after-offset-rollforward-plus-adding-a

Basically, dates before 1677 are not allowed. My guess is that the above is a typo somewhere on FRED's side, and the timestamp should be 1970-01-01. Indeed, when I print the dates approaching the glitch they are as follows: 2003-01-01 2002-01-01 1930-01-01 1997-01-01 1996-01-01 1270-01-01 ^error occurs here

I identified the problem in the following file on line 75: /home/user/anaconda3/lib/python3.7/site-packages/fredapi/fred.py

and the fix I crafted is to add this function to the class:

def _check_date_format(self,date_str):
    datetime = date_str.split('-')
    year = datetime[0]
    if (float(year[1]) < 7) and (float(year[1]) > 0):
        year = list(year)
        year[1] = '9'
    year = ''.join(year)
    datetime = [year,datetime[1],datetime[2]]
    datetime = '-'.join(datetime)
    return datetime

and change the _parse function as follows

def _parse(self, date_str, format='%Y-%m-%d'):
    """
    helper function for parsing FRED date string into datetime
    """
    try:
        rv = pd.to_datetime(date_str, format=format)
    except:
        date_str = self._check_date_format(date_str)
        rv = pd.to_datetime(date_str, format=format)
    if hasattr(rv, 'to_pydatetime'):
        rv = rv.to_pydatetime()
    return rv

I hope that you can add this to the fredapi wrapper code master! This fix should work until 2100 lol.

brandonritchie commented 1 year ago

I am having this issue as well

harttraveller commented 1 year ago

whoops, sorry @brandonritchie try using this: https://github.com/zachwill/fred

ashutoshChant commented 3 weeks ago

faced similar issue while fetching consumer price index