-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_series_preprocessor.py
More file actions
240 lines (204 loc) · 8.88 KB
/
time_series_preprocessor.py
File metadata and controls
240 lines (204 loc) · 8.88 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from typing import Optional, Tuple, Union
import numpy as np
import pandas as pd
class TimeSeriesPreprocessor:
"""
Handles data preprocessing for time series forecasting, including loading, handling duplicates, aligning timestamps, and creating features.
Methods
-------
_handle_duplicate_indices(*dfs)
Averages rows with duplicate indices in one or more pandas DataFrames.
align_timestamps(df1, df2)
Aligns the timestamps of two dataframes, in case they have different frequencies or missing time points.
"""
def __init__(
self,
y_data: Union[pd.DataFrame, pd.Series],
weather_data: Optional[pd.DataFrame] = None,
) -> None:
"""
Constructs all the necessary attributes for the TimeSeriesPreprocessor object.
Parameters
----------
y_data : pd.DataFrame
DataFrame containing the target variable.
weather_data : pd.DataFrame, optional
DataFrame containing the weather data. The default is None.
"""
self.y_data = y_data
self.weather_data = weather_data
# Check if y_data is a pandas DataFrame or Series
if not isinstance(self.y_data, (pd.DataFrame, pd.Series)):
raise TypeError(
"Input y_data must be a pandas DataFrame or Series"
)
# Check if weather_data is a pandas DataFrame or Series
if (
not isinstance(self.weather_data, pd.DataFrame)
and self.weather_data is not None
):
raise TypeError(
"Input weather_data must be a pandas DataFrame or None"
)
@staticmethod
def _handle_missing_indices(
*dfs: Optional[Union[pd.DataFrame, pd.Series]],
freq: Optional[str] = None,
) -> Union[
Union[pd.DataFrame, pd.Series], Tuple[Union[pd.DataFrame, pd.Series]]
]:
"""
Fills missing indices, missing values, and infinite values in one or more pandas DataFrames or Series.
Parameters
----------
dfs : tuple of Union[pd.DataFrame, pd.Series]
One or more pandas DataFrames or Series with timestamps as index.
freq : str, optional
Frequency of the time series data. If None, the frequency will be inferred from the data (default is None).
Returns
-------
output_dfs : Union[pd.DataFrame, pd.Series] or tuple of Union[pd.DataFrame, pd.Series]
DataFrame(s) or Series with missing indices, values, and infinite values filled. If a single DataFrame or Series was passed as input,
a single DataFrame or Series is returned. If multiple DataFrames or Series were passed, a tuple of DataFrames or Series is returned.
"""
output_dfs = []
for df in dfs:
if df is None:
output_dfs.append(None)
continue
# Check if df is a pandas DataFrame or Series
if not isinstance(df, (pd.DataFrame, pd.Series)):
raise TypeError(
"All inputs must be pandas DataFrames or Series"
)
# Check if df has a DatetimeIndex
if not isinstance(df.index, pd.DatetimeIndex):
raise TypeError("All inputs must have a DatetimeIndex")
# If frequency is not provided, infer it from the DataFrame or Series
if freq is None:
freq = pd.infer_freq(df.index)
if freq is None:
raise ValueError(
"Frequency could not be inferred from the data. Please provide the 'freq' parameter."
)
# Create a complete date range for the given frequency
full_range = pd.date_range(
start=df.index.min(), end=df.index.max(), freq=freq
)
# Reindex the DataFrame or Series to ensure all expected indices are present
df = df.reindex(full_range)
# Replace infinite values with NaN
df.replace([np.inf, -np.inf], np.nan, inplace=True)
# Forward fill missing values (including NaN and previously infinite values)
df = df.fillna(method="ffill")
output_dfs.append(df)
if len(output_dfs) == 1:
return output_dfs[0]
else:
return tuple(output_dfs)
@staticmethod
def _handle_duplicate_indices(
*dfs: Optional[Union[pd.DataFrame, pd.Series]]
) -> Union[
Union[pd.DataFrame, pd.Series], Tuple[Union[pd.DataFrame, pd.Series]]
]:
"""
Averages rows with duplicate indices in one or more pandas DataFrames or Series.
Parameters
----------
dfs : tuple of Union[pd.DataFrame, pd.Series]
One or more pandas DataFrames or Series with timestamps as index.
Returns
-------
output_dfs : Union[pd.DataFrame, pd.Series] or tuple of Union[pd.DataFrame, pd.Series]
DataFrame(s) or Series with unique indices. If a single DataFrame or Series was passed as input, a single DataFrame or Series is returned.
If multiple DataFrames or Series were passed, a tuple of DataFrames or Series is returned.
"""
output_dfs = []
for df in dfs:
if df is None:
output_dfs.append(None)
continue
# Check if df is a pandas DataFrame or Series
if not isinstance(df, (pd.DataFrame, pd.Series)):
raise TypeError(
"All inputs must be pandas DataFrames or Series"
)
# Group by index and average rows with duplicate indices
df = df.groupby(df.index).mean()
output_dfs.append(df)
if len(output_dfs) == 1:
return output_dfs[0]
else:
return tuple(output_dfs)
@staticmethod
def _align_timestamps(
*dfs: Optional[Union[pd.DataFrame, pd.Series]], use_union: bool = False
) -> Union[
Optional[Union[pd.DataFrame, pd.Series]],
Tuple[Optional[Union[pd.DataFrame, pd.Series]]],
]:
"""
Aligns the timestamps of multiple dataframes or series, in case they have different frequencies or missing time points.
Parameters
----------
dfs : tuple of Optional[Union[pd.DataFrame, pd.Series]]
One or more pandas DataFrames or Series with timestamps as index, or None.
use_union : bool, optional
If True, aligns the dataframes using the union of indices. If False (default), aligns using the intersection.
Returns
-------
output_dfs : Union[pd.DataFrame, pd.Series] or tuple of Union[pd.DataFrame, pd.Series]
Aligned DataFrame(s) or Series. If a single DataFrame or Series was passed as input, a single DataFrame or Series is returned.
If multiple DataFrames or Series were passed, a tuple of DataFrames or Series is returned.
"""
# Check if any of the inputs are None and handle them appropriately
non_none_dfs = [df for df in dfs if df is not None]
if not non_none_dfs:
raise ValueError(
"At least one DataFrame or Series must be provided."
)
# Find common index across all non-None DataFrames or Series
common_index = non_none_dfs[0].index
for df in non_none_dfs[1:]:
if use_union:
common_index = common_index.union(df.index)
else:
common_index = common_index.intersection(df.index)
# Reindex and let NaNs propagate forward
output_dfs = [
df.reindex(common_index) if df is not None else None for df in dfs
]
if len(output_dfs) == 1:
return output_dfs[0]
else:
return tuple(output_dfs)
def merge_y_and_weather_data(
self, freq: Optional[str] = None, use_union: bool = False
) -> Union[pd.DataFrame, pd.Series]:
"""
Merges y_data and weather_data.
Returns
-------
merged_data : pd.DataFrame
Merged DataFrame.
"""
self.y_data, self.weather_data = self._handle_duplicate_indices( # type: ignore
self.y_data, self.weather_data # type: ignore
)
self.y_data, self.weather_data = self._handle_missing_indices( # type: ignore
self.y_data, self.weather_data, freq=freq # type: ignore
)
self.y_data, self.weather_data = self._align_timestamps( # type: ignore
self.y_data, self.weather_data, use_union=use_union # type: ignore
)
if self.weather_data is None:
return self.y_data # type: ignore
else:
return pd.merge(
self.y_data, # type: ignore
self.weather_data, # type: ignore
left_index=True,
right_index=True,
how="inner",
)