Search code examples
pythonpandasforex

How to find end of quarter values of exchange rates from daily (FRED) quotes?


I am downloading foreign exchange rates for five currency using FRED, see code below.

How do I get end of quarter (year, half-year) values?

import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as pdr
from datetime import datetime

start = datetime(2012, 1, 1)
end   = datetime(2018, 6, 30)
#                        NOK        DKK        Swed        GBP         Euro
fx = pdr.get_data_fred(['DEXNOUS', 'DEXDNUS', 'DEXSDUS' , 'DEXUSUK',  'DEXUSEU'], start=start, end=end)
fx.columns =           ['NOK',     'DKK',     'SEK',      'GBP',      'EUR']
fx.dropna(inplace=True)

I tried resample('A-DEC').mean() for the 4th quarter, but I then cannot match the values with the daily values from FRED.


Solution

  • The problem is your are taking the mean and not using the quarter resampling.

    First create a mock series:

    periods = 10000
    my_index = pd.date_range('2016-07-01', periods=periods, freq='D')
    data = np.random.randint(100,1000,periods)
    orig_ts = pd.Series(data=data, index=my_index, name='daily rates')
    

    Resample it so that it gets the data from last business day of the month. Use resample('Q') for just the last day.

    ts=orig_ts.resample('BQ').backfill()
    print(ts.head())
    
    2016-09-30    173.0
    2016-12-30    243.0
    2017-03-31    209.0
    2017-06-30    124.0
    2017-09-29    817.0
    Freq: BQ-DEC, Name: daily rates, dtype: float64
    

    Check the accuracy:

    print(orig_ts.get_value('2016-09-30'))
    Output: 173