MESQUAL Granularity Modules¶
TimeSeriesGranularityAnalyzer
¶
Analyzes and validates time granularity in DatetimeIndex sequences.
This class provides tools for working with time series data that may have varying granularities (e.g., hourly, quarter-hourly). It's particularly useful for electricity market data analysis where different market products can have different time resolutions.
Features
- Granularity detection for time series data
- Support for mixed granularities within the same series
- Strict mode for validation scenarios
- Per-day granularity analysis
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict_mode
|
bool
|
If True, raises GranularityError when multiple granularities are detected. If False, only issues warnings. Default is True. |
True
|
Example:
>>> analyzer = TimeSeriesGranularityAnalyzer()
>>> index = pd.date_range('2024-01-01', periods=24, freq='h')
>>> analyzer.get_granularity_as_timedelta(index)
Timedelta('1 hours')
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_analyzer.py
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 | |
TimeSeriesGranularityConverter
¶
Converts time series between different granularities while respecting the nature of the quantity.
This class handles the conversion of time series data between different granularities (e.g., hourly to 15-min or vice versa) while properly accounting for the physical nature of the quantity being converted:
-
Intensive quantities (e.g., prices, power levels) are replicated when increasing granularity and averaged when decreasing granularity.
-
Extensive quantities (e.g., volumes, welfare) are split when increasing granularity and summed when decreasing granularity.
Features
- Automatic granularity detection using TimeGranularityAnalyzer
- Per-day processing to handle missing periods properly and prevent incorrect autofilling of missing days
- Support for both intensive and extensive quantities
- Timezone-aware processing including daylight saving transitions
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
__init__
¶
__init__()
Initialize the granularity converter with analyzer instances.
Creates both strict and non-strict granularity analyzers for different validation requirements during conversion operations.
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
51 52 53 54 55 56 57 58 | |
upsample_through_fillna
¶
upsample_through_fillna(data: DataFrame | Series, quantity_type: QuantityTypeEnum) -> DataFrame | Series
Upsample data using forward-fill strategy with quantity-type-aware scaling.
This method handles upsampling of sparse data where some values are missing. It uses forward-fill to propagate values and applies appropriate scaling based on the quantity type:
- For INTENSIVE quantities: Values are replicated without scaling
- For EXTENSIVE quantities: Values are divided by the number of periods they are spread across within each hour-segment group
The method processes data per day and hour to handle missing periods properly and prevent incorrect auto-filling across day boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | Series
|
Time series data to upsample (Series or DataFrame) |
required |
quantity_type
|
QuantityTypeEnum
|
Type of quantity being converted (INTENSIVE or EXTENSIVE) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame | Series
|
Upsampled data with same type as input |
Example:
>>> # For extensive quantities (energy), values are divided
>>> series = pd.Series([100, np.nan, np.nan, np.nan, 200, np.nan, np.nan, np.nan],
... index=pd.date_range('2024-01-01', freq='15min', periods=5))
>>> converter.upsample_through_fillna(series, QuantityTypeEnum.EXTENSIVE)
# Results in [25, 25, 25, 25, 50, 50, 50, 50]
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
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 | |
convert_to_target_index
¶
convert_to_target_index(series: Series, target_index: DatetimeIndex, quantity_type: QuantityTypeEnum) -> Series
Convert a time series to match a specific target DatetimeIndex.
This method converts the granularity of a time series to match the granularity of a target index. The target index must have consistent granularity within each day and consistent granularity across all days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Source time series to convert |
required |
target_index
|
DatetimeIndex
|
DatetimeIndex defining the target granularity and timestamps |
required |
quantity_type
|
QuantityTypeEnum
|
Type of quantity (INTENSIVE or EXTENSIVE) for proper scaling |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Series converted to match target index granularity and timestamps |
Raises:
| Type | Description |
|---|---|
ValueError
|
If target index has multiple granularities within days or inconsistent granularity across days |
Example:
>>> # Convert hourly to 15-min data
>>> hourly_series = pd.Series([100, 150, 200],
... index=pd.date_range('2024-01-01', freq='1H', periods=3))
>>> target_idx = pd.date_range('2024-01-01', freq='15min', periods=12)
>>> result = converter.convert_to_target_index(hourly_series, target_idx,
... QuantityTypeEnum.INTENSIVE)
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
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 | |
convert_to_target_granularity
¶
convert_to_target_granularity(series: Series, target_granularity: Timedelta, quantity_type: QuantityTypeEnum) -> Series
Convert a time series to a specific target granularity.
This method converts the temporal granularity of a time series while properly handling the physical nature of the quantity. The conversion is performed day-by-day to prevent incorrect handling of missing days or daylight saving time transitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Source time series to convert |
required |
target_granularity
|
Timedelta
|
Target granularity as a pandas Timedelta (e.g., pd.Timedelta(minutes=15) for 15-minute data) |
required |
quantity_type
|
QuantityTypeEnum
|
Type of quantity for proper scaling: - INTENSIVE: Values are averaged/replicated (prices, power) - EXTENSIVE: Values are summed/split (volumes, energy) |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Series with converted granularity, maintaining original naming and metadata |
Raises:
| Type | Description |
|---|---|
GranularityConversionError
|
If conversion cannot be performed due to unsupported granularities or data issues |
Example:
>>> # Convert 15-minute to hourly data (downsampling)
>>> quarter_hourly = pd.Series([25, 30, 35, 40],
... index=pd.date_range('2024-01-01', freq='15min', periods=4))
>>> hourly = converter.convert_to_target_granularity(
... quarter_hourly, pd.Timedelta(hours=1), QuantityTypeEnum.EXTENSIVE)
>>> print(hourly) # Result: [130] (25+30+35+40)
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
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 | |
SamplingMethodEnum
¶
Bases: Enum
Enumeration of sampling methods for granularity conversion.
Attributes:
| Name | Type | Description |
|---|---|---|
UPSAMPLING |
Converting from coarser to finer granularity (e.g., hourly to 15-min) |
|
DOWNSAMPLING |
Converting from finer to coarser granularity (e.g., 15-min to hourly) |
|
KEEP |
No conversion needed - source and target granularities are the same |
Source code in submodules/mesqual/mesqual/energy_data_handling/granularity_converter.py
19 20 21 22 23 24 25 26 27 28 29 | |