-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_series_loader.py
More file actions
80 lines (65 loc) · 2.28 KB
/
Copy pathtime_series_loader.py
File metadata and controls
80 lines (65 loc) · 2.28 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from pathlib import Path
from typing import Optional
import pandas as pd
from common_constants import TARGET_VARIABLE, WEATHER_DATA_FILE, Y_FILE
class TimeSeriesLoader:
"""
Handles data loading for time series forecasting.
Attributes
----------
y_file : str
Path of the CSV file containing y data.
weather_data_file : Optional[str]
Path of the CSV file containing weather data.
"""
def __init__(
self,
y_file: str = Y_FILE,
weather_data_file: Optional[str] = WEATHER_DATA_FILE,
) -> None:
"""
Constructs all the necessary attributes for the TimeSeriesLoader object.
Parameters
----------
y_file : str
Path of the CSV file containing y data.
weather_data_file : str
Path of the CSV file containing weather data.
"""
self.y_data, self.weather_data = self.load_data(
y_file, col_name=TARGET_VARIABLE
), self.load_data(weather_data_file)
@staticmethod
def load_data(
file_name: Optional[str], col_name: Optional[str] = None
) -> Optional[pd.DataFrame]:
"""
Load data from CSV files and perform sanity checks.
Parameters
----------
file_name : str
Path of the CSV file containing the data.
Returns
-------
data : pd.DataFrame
Dataframe containing the data.
"""
# If file_name is None, return None
if file_name is None:
return None
# Check if file exists
if not Path(file_name).is_file():
raise FileNotFoundError(f"File {file_name} not found")
# Load data
data = pd.read_csv(file_name, index_col=0)
# Rename index to "DateTime"
data.index.rename("DateTime", inplace=True)
# Convert the index to datetime type
data.index = pd.to_datetime(data.index)
# Check if data is a pd.DataFrame
if not isinstance(data, pd.DataFrame):
raise TypeError("data must be a pandas DataFrame")
# If a column name is specified, and if the dataframe has only one column, rename the column
if col_name is not None and len(data.columns) == 1:
data.columns = [col_name]
return data