ArchCoder/llm-excel-plotter-agent
0
1import pandas as pd2import os3import logging4 5class DataProcessor:6 def __init__(self, data_path=None):7 logging.info("Initializing DataProcessor")8 # Allow dynamic data path (for user uploads), fallback to default9 if data_path and os.path.exists(data_path):10 self.data_path = data_path11 else:12 self.data_path = os.path.join(os.path.dirname(__file__), 'data', 'sample_data.csv')13 self.data = self.load_data(self.data_path)14 15 def load_data(self, path):16 ext = os.path.splitext(path)[1].lower()17 try:18 if ext == '.csv':19 data = pd.read_csv(path)20 elif ext == '.xls':21 data = pd.read_excel(path, engine='xlrd')22 elif ext == '.xlsx':23 data = pd.read_excel(path, engine='openpyxl')24 else:25 raise ValueError(f"Unsupported file type: {ext}")26 logging.info(f"Loaded data from {path} with shape {data.shape}")27 return data28 except Exception as e:29 logging.error(f"Failed to load data: {e}")30 return pd.DataFrame()31 32 def validate_columns(self, required_columns):33 missing = [col for col in required_columns if col not in self.data.columns]34 if missing:35 logging.warning(f"Missing columns: {missing}")36 return False, missing37 return True, []38 39 def get_columns(self):40 return list(self.data.columns)41 42 def preview(self, n=5):43 return self.data.head(n).to_dict(orient='records')44 45 def get_dtypes(self) -> dict:46 result = {}47 for col, dtype in self.data.dtypes.items():48 if pd.api.types.is_integer_dtype(dtype):49 result[col] = "integer"50 elif pd.api.types.is_float_dtype(dtype):51 result[col] = "float"52 elif pd.api.types.is_datetime64_any_dtype(dtype):53 result[col] = "datetime"54 elif pd.api.types.is_bool_dtype(dtype):55 result[col] = "boolean"56 else:57 result[col] = "string"58 return result59 60 def get_stats(self) -> dict:61 numeric = self.data.select_dtypes(include='number')62 if numeric.empty:63 return {}64 desc = numeric.describe().to_dict()65 return {col: {k: round(v, 4) for k, v in stats.items()} for col, stats in desc.items()}66 67 