Ejemplo n.º 1
0
 def write_filter_file(self, df):
     filter_file_split = self.filter_file.split('::')
     filter_file_name = filter_file_split[0]
     if len(filter_file_split) > 1:
         filter_file_cols = filter_file_split[1].split(',')
     else:
         filter_file_cols = []
     df = df[[self.ad_col, self.ad_group_col]]
     ndf = df[[self.ad_col]].drop_duplicates().reset_index(drop=True)
     for col in filter_file_cols:
         col = int(col)
         tdf = pd.DataFrame(df[self.ad_group_col].str.split('_').str[col])
         new_col_name = 'adset_name::{}'.format(col)
         tdf = tdf.rename(columns={self.ad_group_col: new_col_name})
         tdf = tdf.join(df[[self.ad_col
                            ]]).drop_duplicates().reset_index(drop=True)
         fdf = pd.DataFrame()
         for value in tdf[self.ad_col].unique():
             unique_vals = '|'.join(tdf[tdf[self.ad_col] == value]
                                    [new_col_name].unique().tolist())
             value_dict = {
                 new_col_name: [unique_vals],
                 self.ad_col: [value]
             }
             fdf = fdf.append(pd.DataFrame(value_dict),
                              ignore_index=True,
                              sort=False)
         ndf = ndf.merge(fdf, on=self.ad_col)
     ndf = ndf.rename(columns={self.ad_col: 'ad_name'})
     utl.write_df(ndf, file_name=utl.config_file_path + filter_file_name)
Ejemplo n.º 2
0
 def append_and_write_relation_df(self, relation_df):
     df = pd.read_excel(utl.config_file_path + self.relation_file,
                        dtype=object,
                        keep_default_na=False,
                        na_values=[''])
     df = df[~df[Creator.rel_col_imp].
             isin(['creative_filename', 'body', 'description', 'title'])]
     df = df.append(relation_df, ignore_index=True, sort=False)
     df = df.drop_duplicates()
     utl.write_df(df, utl.config_file_path + self.relation_file)
Ejemplo n.º 3
0
 def get_plan_names(self):
     self.col_name = self.col_name.split('|')
     df_dict = {}
     for col in self.col_name:
         if col in self.df.columns:
             df_dict[col] = pd.Series(self.df[col].unique())
         else:
             logging.warning('{} not in df.  Continuing.'.format(col))
     ndf = pd.DataFrame(df_dict)
     utl.write_df(ndf, './' + self.new_file)
Ejemplo n.º 4
0
 def apply_duplication(self):
     cdf = pd.read_excel(self.new_file)
     original_cols = cdf.columns
     duplicated_col = self.col_name.split('::')[0]
     unique_list = cdf[duplicated_col].unique()
     cdf = pd.DataFrame(columns=original_cols)
     self.df = self.df[self.col_name.split('::')[1].split('|')][:]
     for item in unique_list:
         self.df[duplicated_col] = item
         cdf = cdf.append(self.df, ignore_index=True, sort=False)
     cdf = cdf.reset_index(drop=True)
     cdf = cdf[original_cols]
     if len(self.col_name.split('::')) > 2:
         cdf = self.apply_upload_filter(cdf)
     utl.write_df(cdf, self.new_file)
Ejemplo n.º 5
0
 def apply_relations(self):
     cdf = pd.read_excel(self.new_file)
     for imp_col in self.df['impacted_column_name'].unique():
         df = self.df[self.df['impacted_column_name'] == imp_col]
         par_col = str(df['column_name'].values[0]).split('|')
         position = str(df['position'].values[0]).split('|')
         if position == ['Constant']:
             cdf[imp_col] = df['impacted_column_new_value'].values[0]
         else:
             rel_dict = self.create_relation_dictionary(df)
             cdf = self.set_values_to_imp_col(cdf, position, par_col,
                                              imp_col)
             cdf = self.check_undefined_relation(cdf, rel_dict, imp_col)
             cdf[imp_col] = cdf[imp_col].replace(rel_dict)
     utl.write_df(cdf, self.new_file)
     return self.error_dict
Ejemplo n.º 6
0
 def check_undefined_relation(self, df, rel_dict, imp_col):
     undefined = df.loc[~df[imp_col].isin(rel_dict), imp_col]
     imp_file = self.new_file.split('.')[0].replace(file_path, '')
     file_name = utl.err_file_path + imp_file + '_' + imp_col + '.xlsx'
     if not undefined.empty:
         logging.warning('No match found for the following values, '
                         'they were left blank.  An error report was '
                         'generated ' + str(undefined.head().values))
         df.loc[~df[imp_col].isin(rel_dict), imp_col] = ''
         self.error_dict[imp_col] = len(undefined.unique())
         err_file_path = os.path.join(
             *[x for x in file_name.split('/') if '.' not in x])
         utl.dir_check(err_file_path)
         utl.write_df(undefined.drop_duplicates(), file_name)
     else:
         self.error_dict[imp_col] = 0
         utl.remove_file(file_name)
     return df
Ejemplo n.º 7
0
 def write_name_creator_file(self):
     df = pd.DataFrame(self.df[self.ad_col].unique(), columns=[self.ad_col])
     utl.write_df(df, file_name=utl.config_file_path + self.creator_file)
Ejemplo n.º 8
0
 def create_upload_file(self):
     combined_list = self.get_combined_list()
     df = self.create_df(combined_list)
     utl.write_df(df, self.new_file)