Zack commited on
Commit
e62a721
·
1 Parent(s): 03592e6

fix: Add check for existing correct columns

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -60,22 +60,26 @@ def plot_anomalies(df_test_value, data, anomalies):
60
  return fig
61
 
62
  def clean_data(df):
63
- # Check if 'Date' and 'Hour' columns exist in the dataframe
64
- if "Date" in df.columns and "Hour" in df.columns:
 
 
 
 
 
65
  # Convert "Date" and "Hour" columns into datetime format
66
  df["timestamp"] = pd.to_datetime(df["Date"] + ' ' + df["Hour"].astype(str) + ":00:00")
 
67
  # Keep only necessary columns
68
  df = df[["timestamp", "Hourly_Labor_Hours_Total"]]
 
69
  # Rename column
70
  df.rename(columns={"Hourly_Labor_Hours_Total": "value"}, inplace=True)
71
- elif "timestamp" in df.columns:
72
- # If 'timestamp' column exists, rename the value column if necessary
73
- if "Hourly_Labor_Hours_Total" in df.columns:
74
- df.rename(columns={"Hourly_Labor_Hours_Total": "value"}, inplace=True)
75
- df = df[["timestamp", "value"]]
76
  else:
77
- raise ValueError("Input data must have either 'Date' and 'Hour' columns, or a 'timestamp' column.")
78
- return df
79
 
80
  def master(file):
81
  # read file
 
60
  return fig
61
 
62
  def clean_data(df):
63
+ # Check if the DataFrame already contains the correct columns
64
+ if "timestamp" in df.columns and "value" in df.columns:
65
+ df["timestamp"] = pd.to_datetime(df["timestamp"])
66
+ return df
67
+
68
+ # Check if DataFrame contains the columns to be converted
69
+ elif "Date" in df.columns and "Hour" in df.columns and "Hourly_Labor_Hours_Total" in df.columns:
70
  # Convert "Date" and "Hour" columns into datetime format
71
  df["timestamp"] = pd.to_datetime(df["Date"] + ' ' + df["Hour"].astype(str) + ":00:00")
72
+
73
  # Keep only necessary columns
74
  df = df[["timestamp", "Hourly_Labor_Hours_Total"]]
75
+
76
  # Rename column
77
  df.rename(columns={"Hourly_Labor_Hours_Total": "value"}, inplace=True)
78
+
79
+ return df
80
+
 
 
81
  else:
82
+ raise ValueError("Dataframe does not contain necessary columns.")
 
83
 
84
  def master(file):
85
  # read file