def rename(self, mapping=None, inplace=False, append=False,
               check_duplicates=True, **kwargs):
        """Rename and aggregate column entries using `groupby.sum()` on values.
        When renaming models or scenarios, the uniqueness of the index must be
        maintained, and the function will raise an error otherwise.

        Renaming is only applied to any data where a filter matches for all
        columns given in `mapping`. Renaming can only be applied to the `model`
        and `scenario` columns or to other data columns simultaneously.

        Parameters
        ----------
        mapping: dict or kwargs
            mapping of column name to rename-dictionary of that column
            >> {<column_name>: {<current_name_1>: <target_name_1>,
            >>                  <current_name_2>: <target_name_2>}}
            or kwargs as `column_name={<current_name_1>: <target_name_1>, ...}`
        inplace: bool, default False
            if True, do operation inplace and return None
        append: bool, default False
            if True, append renamed timeseries to IamDataFrame
        check_duplicates: bool, default True
            check whether conflict between existing and renamed data exists.
            If True, raise ValueError; if False, rename and merge
            with `groupby().sum()`.
        """
        # combine `mapping` arg and mapping kwargs, ensure no rename conflicts
        mapping = mapping or {}
        duplicate = set(mapping).intersection(kwargs)
        if duplicate:
            msg = 'conflicting rename args for columns `{}`'.format(duplicate)
            raise ValueError(msg)
        mapping.update(kwargs)

        # determine columns that are not `model` or `scenario`
        data_cols = set(self._LONG_IDX) - set(META_IDX)

        # changing index and data columns can cause model-scenario mismatch
        if any(i in mapping for i in META_IDX)\
                and any(i in mapping for i in data_cols):
            msg = 'Renaming index and data cols simultaneously not supported!'
            raise ValueError(msg)

        # translate rename mapping to `filter()` arguments
        filters = {col: _from.keys() for col, _from in mapping.items()}

        # if append is True, downselect and append renamed data
        if append:
            df = self.filter(filters)
            # note that `append(other, inplace=True)` returns None
            return self.append(df.rename(mapping), inplace=inplace)

        # if append is False, iterate over rename mapping and do groupby
        ret = copy.deepcopy(self) if not inplace else self

        # renaming is only applied where a filter matches for all given columns
        rows = ret._apply_filters(**filters)
        idx = ret.meta.index.isin(_make_index(ret.data[rows]))

        # if `check_duplicates`, do the rename on a copy until after the check
        _data = ret.data.copy() if check_duplicates else ret.data

        # apply renaming changes
        for col, _mapping in mapping.items():
            if col in META_IDX:
                _index = pd.DataFrame(index=ret.meta.index).reset_index()
                _index.loc[idx, col] = _index.loc[idx, col].replace(_mapping)
                if _index.duplicated().any():
                    raise ValueError('Renaming to non-unique `{}` index!'
                                     .format(col))
                ret.meta.index = _index.set_index(META_IDX).index
            elif col not in data_cols:
                raise ValueError('Renaming by `{}` not supported!'.format(col))
            _data.loc[rows, col] = _data.loc[rows, col].replace(_mapping)

        # check if duplicates exist between the renamed and not-renamed data
        if check_duplicates:
            merged = (
                _data.loc[rows, self._LONG_IDX].drop_duplicates().append(
                    _data.loc[~rows, self._LONG_IDX].drop_duplicates())
            )
            if any(merged.duplicated()):
                msg = 'Duplicated rows between original and renamed data!\n{}'
                conflict_rows = merged.loc[merged.duplicated(), self._LONG_IDX]
                raise ValueError(msg.format(conflict_rows.drop_duplicates()))

        # merge using `groupby().sum()`
        ret.data = _data.groupby(ret._LONG_IDX).sum().reset_index()

        if not inplace:
            return ret
def rename ( self , mapping = None , inplace = False , append = False , check_duplicates = True , ** kwargs ) : mapping = mapping or { } duplicate = set ( mapping ) . intersection ( kwargs ) if duplicate : msg = 'conflicting rename args for columns `{}`' . format ( duplicate ) raise ValueError ( msg ) mapping . update ( kwargs ) data_cols = set ( self . _LONG_IDX ) - set ( META_IDX ) if any ( i in mapping for i in META_IDX ) and any ( i in mapping for i in data_cols ) : msg = 'Renaming index and data cols simultaneously not supported!' raise ValueError ( msg ) filters = { col : _from . keys ( ) for col , _from in mapping . items ( ) } if append : df = self . filter ( filters ) return self . append ( df . rename ( mapping ) , inplace = inplace ) ret = copy . deepcopy ( self ) if not inplace else self rows = ret . _apply_filters ( ** filters ) idx = ret . meta . index . isin ( _make_index ( ret . data [ rows ] ) ) _data = ret . data . copy ( ) if check_duplicates else ret . data for col , _mapping in mapping . items ( ) : if col in META_IDX : _index = pd . DataFrame ( index = ret . meta . index ) . reset_index ( ) _index . loc [ idx , col ] = _index . loc [ idx , col ] . replace ( _mapping ) if _index . duplicated ( ) . any ( ) : raise ValueError ( 'Renaming to non-unique `{}` index!' . format ( col ) ) ret . meta . index = _index . set_index ( META_IDX ) . index elif col not in data_cols : raise ValueError ( 'Renaming by `{}` not supported!' . format ( col ) ) _data . loc [ rows , col ] = _data . loc [ rows , col ] . replace ( _mapping ) if check_duplicates : merged = ( _data . loc [ rows , self . _LONG_IDX ] . drop_duplicates ( ) . append ( _data . loc [ ~ rows , self . _LONG_IDX ] . drop_duplicates ( ) ) ) if any ( merged . duplicated ( ) ) : msg = 'Duplicated rows between original and renamed data!\n{}' conflict_rows = merged . loc [ merged . duplicated ( ) , self . _LONG_IDX ] raise ValueError ( msg . format ( conflict_rows . drop_duplicates ( ) ) ) ret . data = _data . groupby ( ret . _LONG_IDX ) . sum ( ) . reset_index ( ) if not inplace : return ret
Rename and aggregate column entries using `groupby.sum()` on values.
        When renaming models or scenarios, the uniqueness of the index must be
        maintained, and the function will raise an error otherwise.

        Renaming is only applied to any data where a filter matches for all
        columns given in `mapping`. Renaming can only be applied to the `model`
        and `scenario` columns or to other data columns simultaneously.

        Parameters
        ----------
        mapping: dict or kwargs
            mapping of column name to rename-dictionary of that column
            >> {<column_name>: {<current_name_1>: <target_name_1>,
            >>                  <current_name_2>: <target_name_2>}}
            or kwargs as `column_name={<current_name_1>: <target_name_1>, ...}`
        inplace: bool, default False
            if True, do operation inplace and return None
        append: bool, default False
            if True, append renamed timeseries to IamDataFrame
        check_duplicates: bool, default True
            check whether conflict between existing and renamed data exists.
            If True, raise ValueError; if False, rename and merge
            with `groupby().sum()`.