def check_if_data_exists_at_less_aggregated_geoscale(df, geoscale, activityname): """ In the event data does not exist at specified geoscale, check if data exists at less aggregated level :param df: Either flowbyactivity or flowbysector dataframe :param geoscale: national, state, or county :param activityname: str, activity col names to check :return: str, geoscale to use """ if geoscale == 'national': df = df[(df[fba_activity_fields[0]] == activityname) | (df[fba_activity_fields[1]] == activityname)] fips = create_geoscale_list(df, 'state') df = df[df['Location'].isin(fips)] if len(df) == 0: vLog.info("No flows found for %s at the state scale", activityname) fips = create_geoscale_list(df, 'county') df = df[df['Location'].isin(fips)] if len(df) == 0: vLog.info("No flows found for %s at the county scale", activityname) else: vLog.info( "Flow-By-Activity data exists for %s at " "the county level", activityname) new_geoscale_to_use = 'county' return new_geoscale_to_use else: vLog.info( "Flow-By-Activity data exists for %s at " "the state level", activityname) new_geoscale_to_use = 'state' return new_geoscale_to_use if geoscale == 'state': df = df[(df[fba_activity_fields[0]] == activityname) | (df[fba_activity_fields[1]] == activityname)] fips = create_geoscale_list(df, 'county') df = df[df['Location'].isin(fips)] if len(df) == 0: vLog.info("No flows found for %s at the " "county scale", activityname) else: vLog.info( "Flow-By-Activity data exists for %s " "at the county level", activityname) new_geoscale_to_use = 'county' return new_geoscale_to_use
def check_if_data_exists_at_less_aggregated_geoscale(df, geoscale, activityname): """ In the event data does not exist at specified geoscale, check if data exists at less aggregated level :param df: Either flowbyactivity or flowbysector dataframe :param data_to_check: Either an activity name (ex. 'Domestic') or a sector (ex. '1124') :param geoscale: national, state, or county :param flowbytype: 'fba' for flowbyactivity, 'fbs' for flowbysector :return: """ if geoscale == 'national': df = df[(df[fba_activity_fields[0]] == activityname) | (df[fba_activity_fields[1]] == activityname)] fips = create_geoscale_list(df, 'state') df = df[df['Location'].isin(fips)] if len(df) == 0: log.info("No flows found for " + activityname + " at the state scale.") fips = create_geoscale_list(df, 'county') df = df[df['Location'].isin(fips)] if len(df) == 0: log.info("No flows found for " + activityname + " at the county scale.") else: log.info("Flowbyactivity data exists for " + activityname + " at the county level") new_geoscale_to_use = 'county' return new_geoscale_to_use else: log.info("Flowbyactivity data exists for " + activityname + " at the state level") new_geoscale_to_use = 'state' return new_geoscale_to_use if geoscale == 'state': df = df[(df[fba_activity_fields[0]] == activityname) | (df[fba_activity_fields[1]] == activityname)] fips = create_geoscale_list(df, 'county') df = df[df['Location'].isin(fips)] if len(df) == 0: log.info("No flows found for " + activityname + " at the county scale.") else: log.info("Flowbyactivity data exists for " + activityname + " at the county level") new_geoscale_to_use = 'county' return new_geoscale_to_use
def check_if_data_exists_at_geoscale(df, geoscale, activitynames='All'): """ Check if an activity or a sector exists at the specified geoscale :param df: flowbyactivity dataframe :param activitynames: Either an activity name (ex. 'Domestic') or a sector (ex. '1124') :param geoscale: national, state, or county :return: str, 'yes' or 'no' """ # if any activity name is specified, check if activity data # exists at the specified geoscale activity_list = [] if activitynames != 'All': if isinstance(activitynames, str): activity_list.append(activitynames) else: activity_list = activitynames # check for specified activity name df = df[(df[fba_activity_fields[0]].isin(activity_list)) | (df[fba_activity_fields[1]].isin(activity_list))].reset_index( drop=True) else: activity_list.append('activities') # filter by geoscale depends on Location System fips = create_geoscale_list(df, geoscale) df = df[df['Location'].isin(fips)] if len(df) == 0: vLog.info("No flows found for %s at the %s scale", ', '.join(activity_list), geoscale) exists = "No" else: vLog.info("Flows found for %s at the %s scale", ', '.join(activity_list), geoscale) exists = "Yes" return exists