Exemplo n.º 1
0
    def _insert_deaths_data(self, new_data_df, table_name):
        """
        Inserts deaths data to COVID19 deaths data table, denoted by table_name. If the new data contain
        retrospectively modified rows, such rows are updated in the table

        :param new_data_df: Data frame with new data
        :param table_name: Name of the table to insert data
        :return: The number of updated rows
        """

        changed_rows = -1

        row_count = self.execute_query('SELECT COUNT(*) FROM ' + table_name + ';')[0][0]

        if row_count == 0:
            '''
            The table is empty, insert all the data in the data frame
            '''
            with self.create_connection() as con:
                new_data_df.to_sql(con=con, name=table_name, if_exists='append', index=False)

            changed_rows = len(new_data_df)

        else:
            '''
            If the table is not empty append only the new data rows because re-writing all the 
            data is too expensive
            '''
            # Read the current data from table as a data frame
            with self.create_connection() as con:
                curr_data = pd.read_sql_query('SELECT * FROM ' + table_name + ';', con=con)

            dh = DataHandler()
            # Filter changed or newly added country and date combinations
            modified_rows = dh.get_changed_rows(new_data_df, curr_data)
            # Update the database
            changed_rows = self.upsert_to_table(new_data_df.iloc[modified_rows, ], table_name)

        return changed_rows