def trade_flows(self, from_country=None, to_country=None, sector=None, with_RoW=False): """ Per-sector country-country flows. Calculated from the global import vector and the import propensities. Use `from_country`, `to_country` and `sector` to filter """ imports = self.imports imports = dataframe.filter_pandas(imports, 'sector', sector) imports = dataframe.filter_pandas(imports, 'to_country', to_country) flows = self._split_flows_by_import_propensities( imports, country_field='to_country', with_RoW=with_RoW) flows = dataframe.filter_pandas(flows, 'from_country', from_country) if not with_RoW: flows = dataframe.filter_pandas(flows, ['from_country', 'to_country'], 'RoW', exclude=True) flows.name = 'value' return flows
def _filter_flows(self, flows, country_names=None, sectors=None, with_RoW=False): """ Filter a MultiIndexed pandas object by `country_names` and `sectors`. Both `country_names` and `sectors` can be None, a string or a list of strings. All of `to_country`, `from_country` and `country` are filtered. """ if country_names is None and sectors is None and with_RoW: return flows else: # What should we filter the pandas object on? (index or columns?) if flows.index.names[0] is None: # Dataframe/Series is unindexed names = flows.columns.values.tolist() else: names = flows.index.names # Prepare the filtered pandas object filtered = flows.copy() if country_names is None: country_names = self.country_names # Remove RoW if with_RoW: country_names = country_names + ['RoW'] if sectors is None: sectors = self.sectors # Level names on which to filter sector_levels = [l for l in names if str(l).find('sector') >= 0] country_levels = [l for l in names if str(l).find('country') >= 0] for level in sector_levels: filtered = dataframe.filter_pandas(filtered, level, sectors) for level in country_levels: filtered = dataframe.filter_pandas(filtered, level, country_names) return filtered