|
| 1 | +# vtools/data/indexing.py |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +""" |
| 5 | +Index construction and frequency-handling utilities for time series operations. |
| 6 | +
|
| 7 | +This module provides low-level helpers for: |
| 8 | +- enforcing frequency consistency across multiple time series, |
| 9 | +- constructing regular time indexes from valid data extents, and |
| 10 | +- reindexing existing series onto continuous grids when possible. |
| 11 | +
|
| 12 | +These functions are used by higher-level operations such as merging, |
| 13 | +splicing, and blending of time series. |
| 14 | +""" |
| 15 | + |
| 16 | +def resolve_common_freq(indexes, preserve_freq=True): |
| 17 | + """ |
| 18 | + Determine a common frequency across a collection of pandas indexes. |
| 19 | +
|
| 20 | + This function inspects the `.freq` attribute of each index and, if |
| 21 | + frequency preservation is requested, verifies that all non-null |
| 22 | + frequency attributes are identical. If so, that frequency is returned. |
| 23 | + Otherwise, an error is raised. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + indexes : sequence of pandas.Index |
| 28 | + Sequence of pandas Index objects (typically DatetimeIndex or |
| 29 | + PeriodIndex). Each index may or may not have a `.freq` attribute. |
| 30 | +
|
| 31 | + preserve_freq : bool, default True |
| 32 | + If True, enforce that all indexes with a non-null `.freq` attribute |
| 33 | + have identical frequencies. If any mismatch is detected, a |
| 34 | + ValueError is raised. |
| 35 | +
|
| 36 | + If False, no checking is performed and the function always returns |
| 37 | + None. |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + freq : pandas offset or None |
| 42 | + The common frequency if one can be determined and |
| 43 | + `preserve_freq=True`. Returns None if: |
| 44 | + - `preserve_freq=False`, or |
| 45 | + - no index has a non-null `.freq` attribute. |
| 46 | +
|
| 47 | + Raises |
| 48 | + ------ |
| 49 | + ValueError |
| 50 | + If `preserve_freq=True` and multiple indexes define conflicting |
| 51 | + `.freq` attributes. |
| 52 | +
|
| 53 | + Notes |
| 54 | + ----- |
| 55 | + - This function relies only on the `.freq` attribute and does not |
| 56 | + attempt to infer frequency using `pandas.infer_freq` or the more robust |
| 57 | + vtools functions for that purpose. |
| 58 | + - It is intended for use in routines that require strict consistency |
| 59 | + of sampling intervals across inputs. |
| 60 | +
|
| 61 | + See Also |
| 62 | + -------- |
| 63 | + regular_index_from_valid_extent : Construct a regular index once a |
| 64 | + common frequency has been established. |
| 65 | + """ |
| 66 | + freqs = [idx.freq for idx in indexes if getattr(idx, "freq", None) is not None] |
| 67 | + |
| 68 | + if not preserve_freq or not freqs: |
| 69 | + return None |
| 70 | + |
| 71 | + first = freqs[0] |
| 72 | + for f in freqs[1:]: |
| 73 | + if f != first: |
| 74 | + raise ValueError( |
| 75 | + "Input series have inconsistent frequencies; cannot preserve frequency." |
| 76 | + ) |
| 77 | + return first |
| 78 | + |
| 79 | + |
| 80 | +def regular_index_from_valid_extent(series, freq): |
| 81 | + """ |
| 82 | + Construct a regular index spanning the valid data extent of input series. |
| 83 | +
|
| 84 | + This function creates a regular (fixed-frequency) index that spans |
| 85 | + from the earliest first valid timestamp to the latest last valid |
| 86 | + timestamp across a collection of time series. |
| 87 | +
|
| 88 | + Parameters |
| 89 | + ---------- |
| 90 | + series : sequence of pandas.Series or pandas.DataFrame |
| 91 | + Input time series objects. Each must have an index of the same |
| 92 | + type (DatetimeIndex or PeriodIndex). The index values are used |
| 93 | + to determine the overall time extent of valid data. |
| 94 | +
|
| 95 | + freq : pandas offset |
| 96 | + Frequency to use when constructing the regular index (e.g., |
| 97 | + pandas offset such as Hour, Day, etc.). Typically obtained from |
| 98 | + `resolve_common_freq`. |
| 99 | +
|
| 100 | + Returns |
| 101 | + ------- |
| 102 | + index : pandas.DatetimeIndex or pandas.PeriodIndex |
| 103 | + A regular index spanning from the minimum first valid timestamp |
| 104 | + to the maximum last valid timestamp across all input series. |
| 105 | +
|
| 106 | + If no valid timestamps are found in any series, an empty index |
| 107 | + of the same type as the first input is returned. |
| 108 | +
|
| 109 | + Raises |
| 110 | + ------ |
| 111 | + ValueError |
| 112 | + If the index type is not supported (i.e., not DatetimeIndex or |
| 113 | + PeriodIndex). |
| 114 | +
|
| 115 | + Notes |
| 116 | + ----- |
| 117 | + - The function uses `first_valid_index()` and `last_valid_index()` |
| 118 | + for each series, so leading and trailing NaNs are ignored when |
| 119 | + determining the time extent. |
| 120 | + - The returned index includes both endpoints. |
| 121 | + - No validation is performed to ensure that input series conform |
| 122 | + to the specified frequency. |
| 123 | +
|
| 124 | + See Also |
| 125 | + -------- |
| 126 | + resolve_common_freq : Determine whether a shared frequency exists |
| 127 | + across input indexes. |
| 128 | + """ |
| 129 | + firsts = [s.first_valid_index() for s in series if s.first_valid_index() is not None] |
| 130 | + lasts = [s.last_valid_index() for s in series if s.last_valid_index() is not None] |
| 131 | + |
| 132 | + if not firsts or not lasts: |
| 133 | + return series[0].index[:0] |
| 134 | + |
| 135 | + start = min(firsts) |
| 136 | + end = max(lasts) |
| 137 | + |
| 138 | + idx0 = series[0].index |
| 139 | + if isinstance(idx0, pd.DatetimeIndex): |
| 140 | + return pd.date_range(start=start, end=end, freq=freq) |
| 141 | + elif isinstance(idx0, pd.PeriodIndex): |
| 142 | + return pd.period_range(start=start, end=end, freq=freq) |
| 143 | + else: |
| 144 | + raise ValueError("Unsupported index type for frequency preservation.") |
| 145 | + |
| 146 | + |
| 147 | +def reindex_to_continuous(result, freq): |
| 148 | + """ |
| 149 | + Reindex a time series onto a regular grid if possible. |
| 150 | +
|
| 151 | + This function attempts to map an existing time series onto a |
| 152 | + continuous, fixed-frequency index spanning its full time extent. |
| 153 | + If the existing timestamps are not compatible with the target |
| 154 | + regular grid, the input is returned unchanged. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + result : pandas.Series or pandas.DataFrame |
| 159 | + Time series to be reindexed. Must have a DatetimeIndex or |
| 160 | + PeriodIndex. |
| 161 | +
|
| 162 | + freq : pandas offset or None |
| 163 | + Target frequency for the regular index. If None, no action is |
| 164 | + taken and `result` is returned unchanged. |
| 165 | +
|
| 166 | + Returns |
| 167 | + ------- |
| 168 | + out : pandas.Series or pandas.DataFrame |
| 169 | + Reindexed time series if all existing timestamps align with |
| 170 | + the target regular grid. Otherwise, the original input is |
| 171 | + returned unchanged. |
| 172 | +
|
| 173 | + Notes |
| 174 | + ----- |
| 175 | + - The function first constructs a regular index from the minimum |
| 176 | + to maximum timestamps of `result` using the provided `freq`. |
| 177 | + - If any existing timestamps are not present in the constructed |
| 178 | + regular index, the function does not reindex and instead clears |
| 179 | + the `.freq` attribute (if possible) before returning the original |
| 180 | + data. |
| 181 | + - This behavior is intentionally conservative to avoid silently |
| 182 | + dropping or shifting data. |
| 183 | +
|
| 184 | + - When reindexing succeeds: |
| 185 | + - Missing timestamps are filled with NaN. |
| 186 | + - The `.freq` attribute is set on the resulting index if possible. |
| 187 | +
|
| 188 | + Limitations |
| 189 | + ----------- |
| 190 | + - This function assumes that `result.index` is monotonic and |
| 191 | + comparable with the generated regular index. |
| 192 | + - It does not attempt to infer or repair irregular spacing. |
| 193 | +
|
| 194 | + See Also |
| 195 | + -------- |
| 196 | + regular_index_from_valid_extent : Construct a regular index prior |
| 197 | + to composition operations. |
| 198 | + resolve_common_freq : Determine if a shared frequency can be enforced. |
| 199 | + """ |
| 200 | + if freq is None: |
| 201 | + return result |
| 202 | + |
| 203 | + start = result.index.min() |
| 204 | + end = result.index.max() |
| 205 | + |
| 206 | + if isinstance(result.index, pd.DatetimeIndex): |
| 207 | + cont = pd.date_range(start=start, end=end, freq=freq) |
| 208 | + elif isinstance(result.index, pd.PeriodIndex): |
| 209 | + cont = pd.period_range(start=start, end=end, freq=freq) |
| 210 | + else: |
| 211 | + return result |
| 212 | + |
| 213 | + try: |
| 214 | + if not pd.Index(result.index).isin(cont).all(): |
| 215 | + try: |
| 216 | + result.index.freq = None |
| 217 | + except ValueError: |
| 218 | + pass |
| 219 | + return result |
| 220 | + except Exception: |
| 221 | + return result |
| 222 | + |
| 223 | + result = result.reindex(cont) |
| 224 | + |
| 225 | + try: |
| 226 | + result.index.freq = freq |
| 227 | + except ValueError: |
| 228 | + result.index.freq = None |
| 229 | + return result |
0 commit comments