def test_fetch(self): config_wrapper = loc_utils.use_test_configuration() econ_platform_core.PlatformConfiguration = config_wrapper econ_platform_core.init_package() loc_utils.delete_data_file('TEST_TEST1.txt') ser = econ_platform_core.fetch('TEST@TEST1', database='TEXT') targ = get_test_series('TEST1') self.assertTrue(targ.equals(ser)) self.assertTrue( os.path.exists( os.path.join(os.path.dirname(__file__), 'data', 'TEST_TEST1.txt'))) # fetch again, will be from file. ser = econ_platform_core.fetch('TEST@TEST1', database='TEXT') self.assertTrue(targ.equals(ser))
def test_user_function(self): config_wrapper = loc_utils.use_test_configuration() econ_platform_core.PlatformConfiguration = config_wrapper econ_platform_core.init_package() User_provider = econ_platform_core.Providers.UserProvider User_provider.FunctionMapper['testfn'] = user_function ser = econ_platform_core.fetch('U@testfn(0)', database='TEXT') self.assertEqual(ser.values, [0])
def main(): """ Routine used in testing. Not really a good candidate for test-driven development... :return: """ econ_platform_core.configuration.print_configuration() econ_platform_core.start_log() obj = ProviderCansim_Csv() # obj.ParseUnzipped('10100002') ser = econ_platform_core.fetch('CCSV@10100002|v86822808')
def us_inflation(series_meta): """ :param series_meta: econ_platform_core.SeriesMetadata :return: """ if not str(series_meta.ticker_query) == 'US_CPI_INFLATION': raise ValueError('Wrong series!') cpi_index = fetch('F@CPIAUCSL') # All -items, SA inf = cpi_index.pct_change(12) series_meta.series_name = '12-month Change in U.S. CPI Inflation' series_meta.series_description = 'Calculated series' return inf
def inflation_fn(series_meta, fn_args): """ Inflation dispersion in the U.S., based on major groups food and beverages, housing, apparel, transportation, medical care, recreation, education and communication, and other goods and services fn_args must be of the form N, return_type N = # of months to do the analysis on (annualised inflation rate over N months). return_type = "headline", "min", "max" (case insensitive, do not include quotes). Either returns the headline inflation rate, or the min or max of the major groups. :param series_meta: econ_platform_core.SeriesMetadata :param fn_args': str :return: """ try: N_months, return_type = fn_args.split(',') except: raise ValueError( 'Dispersion function has two arguments: N, return_type') # Remove any quotes from people who don't follow directions... return_type = return_type.replace("'", '') return_type = return_type.replace('"', '') return_type = return_type.strip() return_type = return_type.upper() try: N_months = int(N_months) except: raise ValueError('N_months must be an integer') if return_type not in ('HEADLINE', 'MIN', 'MAX'): raise ValueError( f'Invalid return type {return_type}, must be one of HEADLINE, MIN, MAX' ) if return_type == 'HEADLINE': cpi_index = fetch('F@CPIAUCSL') # All -items, SA pct_chg = cpi_index.pct_change(N_months) factor = 1. + pct_chg annualised = pow(factor, 12. / float(N_months)) annualised = 100. * (annualised - 1.) annualised.name = str(series_meta.ticker_full) series_meta.series_name = 'N-month Annualised Change in U.S. CPI Inflation' series_meta.series_description = 'Calculated series' return annualised # Groups: # food and beverages, housing, apparel, transportation, medical care, recreation, education and communication, and other goods and services groups_tickers = [ 'CPIFABSL', 'CPIHOSSL', 'CPIAPPSL', 'CPITRNSL', 'CPIMEDSL', 'CPIRECSL', 'CPIEDUSL', 'CPIOGSSL' ] group_df = pandas.DataFrame() for group in groups_tickers: level = fetch('F@' + group) pct_chg = level.pct_change(N_months) factor = 1. + pct_chg annualised = pow(factor, 12. / float(N_months)) annualised = 100. * (annualised - 1.) group_df[group] = annualised group_df.dropna(axis=0, inplace=True) if return_type == 'MIN': out = group_df.min(axis=1) series_meta.series_name = f'Minimum {N_months}-annualised inflation for major groups in U.S.' else: out = group_df.max(axis=1) series_meta.series_name = f'Maximum {N_months}-annualised inflation for major groups in U.S.' series_meta.series_description = 'Calculated series' return out, series_meta
factor = 1. + pct_chg annualised = pow(factor, 12. / float(N_months)) annualised = 100. * (annualised - 1.) group_df[group] = annualised group_df.dropna(axis=0, inplace=True) if return_type == 'MIN': out = group_df.min(axis=1) series_meta.series_name = f'Minimum {N_months}-annualised inflation for major groups in U.S.' else: out = group_df.max(axis=1) series_meta.series_name = f'Maximum {N_months}-annualised inflation for major groups in U.S.' series_meta.series_description = 'Calculated series' return out, series_meta # Push the handler into the UserProvider user_provider = Providers.UserProvider user_provider.FunctionMapper['US_INF_DISPERSION'] = inflation_fn # End of code that should be in a library. #-------------------------------------------------------------------- # Now we can fetch it. # Note that since this calculated series uses the US CPI index level, the system # automatically also creates the CPI series on the database when this is called. # Call reset_update_time() to force an update of the series while the code is under development. # Note that this does not affect the series fetched from the external provider. reset_update_time('U@US_INF_DISPERSION(24, MAX)') inf = fetch('U@US_INF_DISPERSION(24, MAX)') quick_plot(inf)
def us_inflation(query_ticker): if not query_ticker == 'US_CPI_INFLATION': raise ValueError('Wrong series!') cpi_index = fetch('F@CPIAUCSL') # All -items, SA inf = cpi_index.pct_change(12) return inf
""" from econ_platform_core import fetch, Providers from econ_platform.start import quick_plot #--------------------------------------------------------------------------------------- # Normally, the user-defined series would be handled in a library. Kept here to keep # Example self-contained. def us_inflation(query_ticker): if not query_ticker == 'US_CPI_INFLATION': raise ValueError('Wrong series!') cpi_index = fetch('F@CPIAUCSL') # All -items, SA inf = cpi_index.pct_change(12) return inf # Push the handler into the UserProvider # Assume we are still using the default provider code user_provider = Providers['U'] user_provider.SeriesMapper['US_CPI_INFLATION'] = us_inflation # End of code that should be in a library. #-------------------------------------------------------------------- # Now we can fetch it. # Note that since this calculated series uses the US CPI index level, the system # automatically also creates the CPI series on the database when this is called. inf = fetch('U@US_CPI_INFLATION') quick_plot(inf)
:param series_meta: econ_platform_core.SeriesMetadata :return: """ if not str(series_meta.ticker_query) in ('GREEK_DRACHMA_OVERNIGHT_AVG', 'GREEK_DRACHMA_OVERNIGHT_EOM'): raise ValueError('Wrong series!') full_ticker = tickers.TickerFull(series_meta.ticker_full) # I will assume that the file is downloaded to the current directory sheet = pandas.read_excel('drachma_overnight_rate.xls', header=2) if str(series_meta.ticker_query) == 'GREEK_DRACHMA_OVERNIGHT_AVG': ser = pandas.Series(sheet['monthly averages']) ser.index = sheet['Unnamed: 0'] series_meta.series_name = 'Average pre-Euro Greek Drachma Bank Overnight Rate' series_meta.series_description = 'Monthly average bank overnight rate from Bank of Greece' # quick_plot(ser) else: print(sheet) raise NotImplementedError('Bam!') ser.name = str(full_ticker) return ser # Push the handler into the UserProvider user_provider = Providers.UserProvider user_provider.SeriesMapper['GREEK_DRACHMA_OVERNIGHT_AVG'] = greek_overnight user_provider.SeriesMapper['GREEK_DRACHMA_OVERNIGHT_EOM'] = greek_overnight # End of code that should be in a library. #-------------------------------------------------------------------- # Now we can fetch it. ser = fetch('U@GREEK_DRACHMA_OVERNIGHT_AVG') quick_plot(ser)