def rename_item(self, table_name, item_old_name, item_new_name):
        """
        Rename item (such as a site) everywhere that it occurs.
        This change often spans multiple tables.
        For example, a site name will occur in the sites table,
        the samples table, and possibly in the locations/ages tables.
        """
        # define some helper methods:

        def put_together_if_list(item):
            """
            String joining function
            that doesn't break with None/np.nan
            """
            try:
                res = ":".join(item)
                return ":".join(item)
            except TypeError as ex:
                #print ex
                return item

        def replace_colon_delimited_value(df, col_name, old_value, new_value):
            """
            Col must contain list
            """
            count = 1
            for index, row in df[df[col_name].notnull()].iterrows():
                names_list = row[col_name]
                names_list = [name.strip() for name in names_list]
                try:
                    ind = names_list.index(old_value)
                except ValueError as ex:
                    count += 1
                    continue
                names_list[ind] = new_value
                df.loc[count, col_name] = names_list
                count += 1

        # initialize some things
        item_type = table_name
        ###col_name = item_type[:-1] + "_name"
        col_name = item_type[:-1]
        col_name_plural = col_name + "s"
        table_df = self.tables[item_type].df

        if item_old_name == '':
            # just add a new item
            self.add_item(table_name, {col_name: item_new_name}, item_new_name)
            return

        # rename item in its own table
        table_df.rename(index={item_old_name: item_new_name}, inplace=True)
        # rename in any parent/child tables
        for table_name in self.tables:
            df = self.tables[table_name].df
            col_names = df.columns
            # change anywhere col_name (singular, i.e. site) is found
            if col_name in col_names:
                df[col_name].where(df[col_name] != item_old_name, item_new_name, inplace=True)
                # change anywhere col_name (plural, i.e. sites) is found
            if col_name_plural in col_names:
                df[col_name_plural + "_list"] = df[col_name_plural].str.split(":")
                replace_colon_delimited_value(df, col_name_plural + "_list", item_old_name, item_new_name)
                df[col_name_plural] = df[col_name_plural + "_list"].apply(put_together_if_list)
                df.drop(col_name_plural + "_list", axis=1, inplace=True)
            self.tables[table_name].df = df
def rename_item ( self , table_name , item_old_name , item_new_name ) : def put_together_if_list ( item ) : try : res = ":" . join ( item ) return ":" . join ( item ) except TypeError as ex : return item def replace_colon_delimited_value ( df , col_name , old_value , new_value ) : count = 1 for index , row in df [ df [ col_name ] . notnull ( ) ] . iterrows ( ) : names_list = row [ col_name ] names_list = [ name . strip ( ) for name in names_list ] try : ind = names_list . index ( old_value ) except ValueError as ex : count += 1 continue names_list [ ind ] = new_value df . loc [ count , col_name ] = names_list count += 1 item_type = table_name col_name = item_type [ : - 1 ] col_name_plural = col_name + "s" table_df = self . tables [ item_type ] . df if item_old_name == '' : self . add_item ( table_name , { col_name : item_new_name } , item_new_name ) return table_df . rename ( index = { item_old_name : item_new_name } , inplace = True ) for table_name in self . tables : df = self . tables [ table_name ] . df col_names = df . columns if col_name in col_names : df [ col_name ] . where ( df [ col_name ] != item_old_name , item_new_name , inplace = True ) if col_name_plural in col_names : df [ col_name_plural + "_list" ] = df [ col_name_plural ] . str . split ( ":" ) replace_colon_delimited_value ( df , col_name_plural + "_list" , item_old_name , item_new_name ) df [ col_name_plural ] = df [ col_name_plural + "_list" ] . apply ( put_together_if_list ) df . drop ( col_name_plural + "_list" , axis = 1 , inplace = True ) self . tables [ table_name ] . df = df
Rename item (such as a site) everywhere that it occurs.
        This change often spans multiple tables.
        For example, a site name will occur in the sites table,
        the samples table, and possibly in the locations/ages tables.