Infer the most likely frequency given the input index. Parameters ---------- index : CFTimeIndex, DataArray, DatetimeIndex, TimedeltaIndex, Series If not passed a CFTimeIndex, this simply calls `pandas.infer_freq`. If passed a Series or a DataArray will use the valu
(index)
| 59 | |
| 60 | |
| 61 | def infer_freq(index): |
| 62 | """ |
| 63 | Infer the most likely frequency given the input index. |
| 64 | |
| 65 | Parameters |
| 66 | ---------- |
| 67 | index : CFTimeIndex, DataArray, DatetimeIndex, TimedeltaIndex, Series |
| 68 | If not passed a CFTimeIndex, this simply calls `pandas.infer_freq`. |
| 69 | If passed a Series or a DataArray will use the values of the series (NOT THE INDEX). |
| 70 | |
| 71 | Returns |
| 72 | ------- |
| 73 | str or None |
| 74 | None if no discernible frequency. |
| 75 | |
| 76 | Raises |
| 77 | ------ |
| 78 | TypeError |
| 79 | If the index is not datetime-like. |
| 80 | ValueError |
| 81 | If there are fewer than three values or the index is not 1D. |
| 82 | """ |
| 83 | from xarray.core.dataarray import DataArray |
| 84 | from xarray.core.variable import Variable |
| 85 | |
| 86 | if isinstance(index, DataArray | pd.Series): |
| 87 | if index.ndim != 1: |
| 88 | raise ValueError("'index' must be 1D") |
| 89 | elif not _contains_datetime_like_objects(Variable("dim", index)): |
| 90 | raise ValueError("'index' must contain datetime-like objects") |
| 91 | dtype = np.asarray(index).dtype |
| 92 | |
| 93 | if _is_numpy_subdtype(dtype, "datetime64"): |
| 94 | index = pd.DatetimeIndex(index.values) |
| 95 | elif _is_numpy_subdtype(dtype, "timedelta64"): |
| 96 | index = pd.TimedeltaIndex(index.values) |
| 97 | else: |
| 98 | index = CFTimeIndex(index.values) |
| 99 | |
| 100 | if isinstance(index, CFTimeIndex): |
| 101 | inferer = _CFTimeFrequencyInferer(index) |
| 102 | return inferer.get_freq() |
| 103 | |
| 104 | return _legacy_to_new_freq(pd.infer_freq(index)) |
| 105 | |
| 106 | |
| 107 | class _CFTimeFrequencyInferer: # (pd.tseries.frequencies._FrequencyInferer): |
searching dependent graphs…