myspace-ooty-analytics / src /data_loading.py
abraham9486937737
Deploy MySpace Ooty Analytics to Hugging Face - with KPI styling updates
04b129a
"""
Data loading module for reading Excel and CSV files
"""
import pandas as pd
import os
from pathlib import Path
from typing import Union, Optional
def load_excel_data(file_path: Union[str, Path], sheet_name: Optional[str] = None) -> pd.DataFrame:
"""
Load Excel file data
Args:
file_path: Path to Excel file
sheet_name: Name of sheet to load (if None, loads first sheet)
Returns:
DataFrame containing the data
"""
try:
df = pd.read_excel(file_path, sheet_name=sheet_name)
print(f"βœ“ Successfully loaded: {file_path}")
return df
except Exception as e:
print(f"βœ— Error loading file: {e}")
raise
def load_csv_data(file_path: Union[str, Path]) -> pd.DataFrame:
"""
Load CSV file data
Args:
file_path: Path to CSV file
Returns:
DataFrame containing the data
"""
try:
df = pd.read_csv(file_path)
print(f"βœ“ Successfully loaded: {file_path}")
return df
except Exception as e:
print(f"βœ— Error loading file: {e}")
raise
def get_data_files(directory: Union[str, Path], file_type: str = "xlsx") -> list:
"""
Get all data files of specific type from directory
Args:
directory: Path to directory
file_type: File extension to search for
Returns:
List of file paths
"""
path = Path(directory)
files = list(path.glob(f"*.{file_type}"))
return sorted(files)