Spaces:
Sleeping
Sleeping
Download data_processor.py from friedeggy/Final_Project: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/friedeggy/Final_Project/resolve/main/data_processor.py
- Command line
-
hf download hf://spaces/friedeggy/Final_Project/data_processor.py
-
curl -L -o data_processor.py https://huggingface.co/spaces/friedeggy/Final_Project/resolve/main/data_processor.py
3.1 kB
| """ | |
| Ashley Wu | |
| Processes data for app | |
| """ | |
| import pandas as pd | |
| import numpy as np | |
| import gradio as gr | |
| import datetime | |
| import utils | |
| #TODO: Add data preview and also do some output shenanigans, but that can wait for a little bit | |
| def process_csv(file): | |
| """ | |
| Create and clean dataframe | |
| Args: | |
| file: The uploaded file object from Gradio | |
| Returns: | |
| A pandas DataFrame to display in Gradio | |
| """ | |
| if file is None: | |
| return "No file uploaded" | |
| try: | |
| # Read the CSV file | |
| df = pd.read_csv(file.name) | |
| # Detect Null values | |
| null_data = df.isnull().sum() | |
| if df.isnull().values.any(): | |
| gr.Warning("Null data detected! Removing rows with null values", duration=5) | |
| df = df.dropna() | |
| df_basics = f"The shape of this dataframe is {str(df.shape)} \n {df.describe()} \n Null data: \n {null_data}" | |
| # Return the dataframe for display | |
| return df, df_basics | |
| except Exception as e: | |
| raise gr.Error(f"Error reading CSV: {str(e)}", duration=10) | |
| def save_csv(dataframe): | |
| """ | |
| Save the modified dataframe to a CSV file. | |
| Args: | |
| dataframe: The modified pandas DataFrame | |
| Returns: | |
| Path to the saved CSV file | |
| """ | |
| if dataframe is None: | |
| return None | |
| output_path = "modified_data.csv" | |
| dataframe.to_csv(output_path, index=False) | |
| return output_path | |
| def set_max(max_value, df, column): | |
| """ | |
| Docstring for update_max | |
| :param df: Description | |
| :param max_value: Description | |
| """ | |
| filtered_df = df[df[column] < max_value] | |
| return filtered_df | |
| def set_min(min_value, df, column): | |
| """ | |
| Docstring for update_max | |
| :param df: Description | |
| :param max_value: Description | |
| """ | |
| filtered_df = df[df[column] > min_value] | |
| return filtered_df | |
| def set_categories(removals, df, column): | |
| """ | |
| Removes the selected element's row from the dataframe | |
| Args: | |
| removals: list of elements to be removed | |
| df: dataframe | |
| column: selected column to find values of | |
| """ | |
| df = utils.detect_categories(df) | |
| for row in removals: | |
| df = df[df[column] != row] | |
| return df | |
| def submit_dates(df, max_date, min_date, column): | |
| """ | |
| Filters all dates larger than the max_date and lower than the min_date | |
| Args: | |
| max_date: maximum date | |
| df: dataframe | |
| column: column of dataframe to filter | |
| Returns: | |
| filtered_df: filtered dataframe | |
| """ | |
| df = utils.detect_dates(df) | |
| filtered_df = df | |
| try: | |
| if max_date is not None: | |
| filtered_df = filtered_df[filtered_df[column] <= max_date] | |
| if min_date is not None: | |
| filtered_df = filtered_df[filtered_df[column] >= min_date] | |
| except: | |
| raise gr.Error("The max date is not in the correct format", duration=1) | |
| return filtered_df | |