File size: 3,103 Bytes
6f6318e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""

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