MCPcopy Index your code
hub / github.com/pydata/xarray / infer_freq

Function infer_freq

xarray/coding/frequencies.py:61–104  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

59
60
61def 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
107class _CFTimeFrequencyInferer: # (pd.tseries.frequencies._FrequencyInferer):

Callers 4

date_range_likeFunction · 0.90
freqMethod · 0.90
test_date_range_likeFunction · 0.90

Calls 7

VariableClass · 0.90
_is_numpy_subdtypeFunction · 0.90
CFTimeIndexClass · 0.90
_legacy_to_new_freqFunction · 0.90
get_freqMethod · 0.80

Tested by 2

test_date_range_likeFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…