def set_key(df, key): """ Set the key attribute for a dataframe Args: df (pandas dataframe): Dataframe for which the key must be set key (str): Key attribute in the dataframe Returns: status (bool). Returns True if the key attribute was set successfully, else returns False """ if not isinstance(df, pd.DataFrame): logger.error('Input object is not of type pandas data frame') raise AssertionError('Input object is not of type pandas data frame') if not key in df.columns: logger.error('Input key ( %s ) not in the dataframe' % key) raise KeyError('Input key ( %s ) not in the dataframe' % key) if ch.is_key_attribute(df, key) is False: logger.warning('Attribute (' + key + ') does not qualify to be a key; ' 'Not setting/replacing the key') return False else: return set_property(df, 'key', key)
def set_key(df, key): """ Set the key attribute for a dataframe Args: df (pandas dataframe): Dataframe for which the key must be set key (str): Key attribute in the dataframe Returns: status (bool). Returns True if the key attribute was set successfully, else returns False """ if not isinstance(df, pd.DataFrame): logger.error('Input object is not of type pandas data frame') raise AssertionError('Input object is not of type pandas data frame') if not key in df.columns: logger.error('Input key ( %s ) not in the dataframe' % key) raise KeyError('Input key ( %s ) not in the dataframe' % key) if ch.is_key_attribute(df, key) is False: logger.warning( 'Attribute (' + key + ') does not qualify to be a key; ' 'Not setting/replacing the key') return False else: return set_property(df, 'key', key)
def validate_metadata_for_table(table, key, out_str, lgr, verbose): if not isinstance(table, pd.DataFrame): logger.error('Input object is not of type pandas data frame') raise AssertionError('Input object is not of type pandas data frame') if not key in table.columns: logger.error('Input key ( %s ) not in the dataframe' % key) raise KeyError('Input key ( %s ) not in the dataframe' % key) ch.log_info(lgr, 'Validating ' + out_str + ' key: ' + str(key), verbose) assert isinstance(key, six.string_types) is True, 'Key attribute must be a string.' assert ch.check_attrs_present(table, key) is True, 'Key attribute is not present in the ' + out_str + ' table' assert ch.is_key_attribute(table, key, verbose) == True, 'Attribute ' + str(key) + \ ' in the ' + out_str + ' table ' \ 'does not qualify to be the key' ch.log_info(lgr, '..... Done', verbose) return True
def validate_metadata_for_table(table, key, out_str, lgr, verbose): if not isinstance(table, pd.DataFrame): logger.error('Input object is not of type pandas data frame') raise AssertionError('Input object is not of type pandas data frame') if not key in table.columns: logger.error('Input key ( %s ) not in the dataframe' % key) raise KeyError('Input key ( %s ) not in the dataframe' % key) ch.log_info(lgr, 'Validating ' + out_str + ' key: ' + str(key), verbose) assert isinstance( key, six.string_types) is True, 'Key attribute must be a string.' assert ch.check_attrs_present( table, key ) is True, 'Key attribute is not present in the ' + out_str + ' table' assert ch.is_key_attribute(table, key, verbose) == True, 'Attribute ' + str(key) + \ ' in the ' + out_str + ' table ' \ 'does not qualify to be the key' ch.log_info(lgr, '..... Done', verbose) return True
def test_is_key_attribute_invalid_attr(self): A = pd.read_csv(path_a) ch.is_key_attribute(A, None)
def test_is_key_attribute_invalid_df(self): ch.is_key_attribute(None, 'id')
def test_is_key_attribute_valid_4(self): A = pd.DataFrame(columns=['id', 'name']) status = ch.is_key_attribute(A, 'id') self.assertEqual(status, True)
def test_is_key_attribute_valid_3(self): p = os.sep.join([catalog_datasets_path, 'A_mvals.csv']) A = pd.read_csv(p) status = ch.is_key_attribute(A, 'ID', True) self.assertEqual(status, False)
def test_is_key_attribute_valid_2(self): A = pd.read_csv(path_a) status = ch.is_key_attribute(A, 'zipcode', True) self.assertEqual(status, False)
def test_is_key_attribute_valid_1(self): A = pd.read_csv(path_a) status = ch.is_key_attribute(A, 'ID', True) self.assertEqual(status, True)