def test_dsct(): population = create_empty_population_dataframe(2001, 2061) cohorts = Cohorts(data=population, columns=['pop']) cohorts.gen_dsct(0.05) test_value = cohorts.get_value((0, 0, 2060), 'dsct') # print test_value assert test_value <= 1
def test_dsct(): population = create_empty_population_dataframe(2001, 2061) cohorts = Cohorts(data=population, columns=["pop"]) cohorts.gen_dsct(0.05) test_value = cohorts.get_value((0, 0, 2060), "dsct") # print test_value assert test_value <= 1
def population_project(self, year_length=None, method=None, growth_rate=None): """ Continuation of population to provide convergent present values Parameters ---------- year_length : int, default None Duration to continue the population projection method : str, default None The value must be 'stable' or 'exp_growth' """ if 'pop' not in self.columns: raise Exception('pop is not a column of cohort') if year_length is None: raise Exception('a duration in years should be provided') if method is None: raise Exception('a method should be specified') years = self.index_sets['year'] first_year = min(years) last_year = max(years) if (first_year + year_length) > last_year: new_last_year = first_year + year_length else: return if method == 'stable': last_pop = self.xs(last_year, level='year', axis=0) pop = DataFrame(self['pop']) years = range(last_year + 1, new_last_year + 1) list_df = [last_pop] * len(years) pop = concat(list_df, keys=years, names=['year']) pop = pop.reorder_levels(['age', 'sex', 'year'], axis=0) combined = self.combine_first(pop) self.__init__(data=combined, columns=['pop']) if method == 'exp_growth': if growth_rate is None: raise Exception( 'a growth rate must be provided for the method') last_pop = self.xs(last_year, level='year', axis=0) pop = DataFrame(self['pop']) years = range(last_year + 1, new_last_year + 1) list_df = [last_pop] * len(years) pop = concat(list_df, keys=years, names=['year']) pop = pop.reorder_levels(['age', 'sex', 'year'], axis=0) pop = Cohorts(pop) pop.gen_grth(growth_rate) pop['pop'] *= pop['grth'] del pop['grth'] combined = self.combine_first(pop) self.__init__(data=combined, columns=['pop'])
def extract_generation(self, year, typ, age = None): """ Returns a dataframe containning chosen data to follow the evolution of a given group over time. Parameters ---------- year : Int A year of reference contained in the cohort. typ : Str A column or a list of columns of the cohort that one wants to follow age : Int Default is zero. The age of reference of the group one is interested in. Returns ------- generation_cohort : a cohort dataframe with the data of the group of people whose given references belong to. """ if year is None: raise Exception('a year of reference is needed') if year not in list(self.index_sets['year']): raise Exception('The given year is not valid') year_min = array(list(self.index_sets['year'])).min() year_max = array(list(self.index_sets['year'])).max() if typ not in self.columns: raise Exception('the given column is not in the cohort') #Normalizing the age if not given if age is None: age = 0 pvm = self.xs(0, level='sex') pvf = self.xs(1, level='sex') #Creating lower bounds for the filter generation if age > 0: if year-age >= year_min: start_age = 0 year_start = year - age else: start_age = age - (year - year_min) year_start = year_min else: start_age = age year_start = year #Creating upper bounds for filter_list if year + (100-age) >= year_max: year_end = year_max end_age = age + (year_max - year) else: year_end = year + 100 - age end_age = 100 #Creating the filtering list filter_list = zip(range(start_age, end_age+1), range(year_start, year_end+1)) # Generation the generation DataFrame generation_data_male = pvm.loc[filter_list, typ] generation_data_female = pvf.loc[filter_list, typ] pieces = [generation_data_male, generation_data_female] res = concat(pieces, keys = [0,1], names = ["sex"] ) res = res.reorder_levels(['age','sex','year']) generation_cohort = Cohorts(res) generation_cohort.columns = [typ] return generation_cohort