Skip to content

Latest commit

 

History

History
89 lines (79 loc) · 4.18 KB

File metadata and controls

89 lines (79 loc) · 4.18 KB

Exception Handling in DataframeAnalyzer

Rationale for Exception Handling

In the DataframeAnalyzer class, exceptions are handled to ensure the program continues to run smoothly even when unexpected issues occur. Proper exception handling helps in debugging and maintaining the robustness of the application. Here are the two main places where we implemented exception handling:

1. adjust_datatypes Method

Reason for Try-Except Block:

  • This method converts the datatypes of several columns. If any of these columns are missing or named incorrectly, a KeyError will be raised.
  • Additionally, other unexpected errors might occur during the conversion process.

Exception Handling:

  • KeyError: Catches and prints an error message specifying which column is missing.
  • General Exception: Catches any other unexpected errors and prints an error message.
def adjust_datatypes(self, df):
    try:
        # Datatype conversions
        df['who_region'] = df['who_region'].astype('string')
        df['iso3'] = df['iso3'].astype('string')
        df['country_name'] = df['country_name'].astype('string')
        df['city'] = df['city'].astype('string')
        df['year'] = df['year'].astype('Int64')
        df['version'] = df['version'].astype('string')
        df['pm10_concentration'] = df['pm10_concentration'].astype('Float64')
        df['pm25_concentration'] = df['pm25_concentration'].astype('Float64')
        df['no2_concentration'] = df['no2_concentration'].astype('Float64')
        df['pm10_tempcov'] = df['pm10_tempcov'].astype('Int64')
        df['pm25_tempcov'] = df['pm25_tempcov'].astype('Int64')
        df['no2_tempcov'] = df['no2_tempcov'].astype('Int64')
        df['type_of_stations'] = df['type_of_stations'].astype('string')
        df['reference'] = df['reference'].astype('string')
        df['web_link'] = df['web_link'].astype('string')
        df['population'] = df['population'].astype('Int64')
        df['population_source'] = df['population_source'].astype('string')
        df['latitude'] = df['latitude'].astype('Float64')
        df['longitude'] = df['longitude'].astype('Float64')
        df['who_ms'] = df['who_ms'].astype('Int64')
        df['city'] = df['city'].str.split('/').str[0]
        return df
    except KeyError as e:
        print(f"KeyError: One of the columns is missing - {e}")
        return df
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return df

2. get_lines_by_country_and_year Method

Reason for Try-Except Block:

  • This method filters the dataframe based on the provided country and year. If the columns used for filtering are missing or misnamed, a KeyError will be raised.
  • Similar to the previous method, other unexpected errors might occur.

Exception Handling:

  • KeyError: Catches and prints an error message specifying which column is missing.
  • General Exception: Catches any other unexpected errors and prints an error message.
def get_lines_by_country_and_year(self, country, year):
    try:
        df = self.working_dataframe[
            (self.working_dataframe['country_name'] == country) & (self.working_dataframe['year'] == year)]
        return df[['pm10_concentration', 'pm25_concentration', 'no2_concentration', 'pm10_tempcov', 'pm25_tempcov',
                   'no2_tempcov', 'population', 'city']]
    except KeyError as e:
        print(f"KeyError: One of the columns is missing - {e}")
        return pd.DataFrame()
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
        return pd.DataFrame()

3. execute_bubble Method

Here we ask the user to input a year. It is important that the input is a number that we can cast to int. We try to cast the input-string to int. If it doesnt work we catch the error and let the user know and give him a second chance.

        while not bool_year:
            print("Which year are you interested in? (q for quit)")
            year = input("Your choice : ")
            if year == "q":
                exit()
            try:
                year_int = int(year)
                bool_year = True
            except ValueError:
                print("Invalid input. Please enter a number.")