File size: 1,531 Bytes
04b129a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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)