MaheshP98 commited on
Commit
bfc7a88
·
verified ·
1 Parent(s): 7037721

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -226,14 +226,17 @@ def validate_csv(df):
226
  Validate that the CSV has the required columns.
227
  Returns True if valid, False otherwise with an error message.
228
  """
 
 
229
  required_columns = ['device_id', 'usage_hours', 'amc_date', 'status']
230
  missing_columns = [col for col in required_columns if col not in df.columns]
231
  if missing_columns:
232
- return False, f"Missing required columns: {', '.join(missing_columns)}"
233
  # Validate data types
234
  try:
235
  df['usage_hours'] = pd.to_numeric(df['usage_hours'], errors='raise')
236
- df['amc_date'] = pd.to_datetime(df['amc_date'], errors='raise')
 
237
  # Handle 'downtime' if present
238
  if 'downtime' in df.columns:
239
  df['downtime'] = pd.to_numeric(df['downtime'], errors='raise')
@@ -370,8 +373,10 @@ def process_files(uploaded_files):
370
  # Load and combine CSV files
371
  for file in valid_files:
372
  try:
373
- df = pd.read_csv(file.name)
374
- logging.info(f"Loaded {len(df)} records from {file.name}")
 
 
375
  # Rename columns to match expected names
376
  df = df.rename(columns={
377
  'device_id': 'equipment',
 
226
  Validate that the CSV has the required columns.
227
  Returns True if valid, False otherwise with an error message.
228
  """
229
+ # Strip whitespace from column names
230
+ df.columns = df.columns.str.strip()
231
  required_columns = ['device_id', 'usage_hours', 'amc_date', 'status']
232
  missing_columns = [col for col in required_columns if col not in df.columns]
233
  if missing_columns:
234
+ return False, f"Missing required columns: {', '.join(missing_columns)}. Found columns: {', '.join(df.columns)}"
235
  # Validate data types
236
  try:
237
  df['usage_hours'] = pd.to_numeric(df['usage_hours'], errors='raise')
238
+ # Parse amc_date with specified format
239
+ df['amc_date'] = pd.to_datetime(df['amc_date'], format='%d-%m-%Y', errors='raise')
240
  # Handle 'downtime' if present
241
  if 'downtime' in df.columns:
242
  df['downtime'] = pd.to_numeric(df['downtime'], errors='raise')
 
373
  # Load and combine CSV files
374
  for file in valid_files:
375
  try:
376
+ # Read CSV with explicit delimiter and strip whitespace
377
+ df = pd.read_csv(file.name, delimiter=',', skipinitialspace=True)
378
+ # Log the columns for debugging
379
+ logging.info(f"Columns in {file.name}: {', '.join(df.columns)}")
380
  # Rename columns to match expected names
381
  df = df.rename(columns={
382
  'device_id': 'equipment',