|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import TYPE_CHECKING |
| 3 | +from typing import ( |
| 4 | + TYPE_CHECKING, |
| 5 | + cast, |
| 6 | +) |
4 | 7 |
|
5 | 8 | import numpy as np
|
6 | 9 |
|
|
51 | 54 | Series,
|
52 | 55 | TimedeltaIndex,
|
53 | 56 | )
|
| 57 | + from pandas.core.arrays import TimedeltaArray |
54 | 58 | from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
|
55 | 59 | # ---------------------------------------------------------------------
|
56 | 60 | # Offset names ("time rules") and related functions
|
@@ -140,10 +144,7 @@ def infer_freq(
|
140 | 144 | >>> pd.infer_freq(idx)
|
141 | 145 | 'D'
|
142 | 146 | """
|
143 |
| - from pandas.core.api import ( |
144 |
| - DatetimeIndex, |
145 |
| - Index, |
146 |
| - ) |
| 147 | + from pandas.core.api import DatetimeIndex |
147 | 148 |
|
148 | 149 | if isinstance(index, ABCSeries):
|
149 | 150 | values = index._values
|
@@ -172,15 +173,14 @@ def infer_freq(
|
172 | 173 | inferer = _TimedeltaFrequencyInferer(index)
|
173 | 174 | return inferer.get_freq()
|
174 | 175 |
|
175 |
| - if isinstance(index, Index) and not isinstance(index, DatetimeIndex): |
| 176 | + if not hasattr(index, "dtype"): |
| 177 | + pass |
| 178 | + elif isinstance(index.dtype, object): |
176 | 179 | if is_numeric_dtype(index.dtype):
|
177 | 180 | raise TypeError(
|
178 | 181 | f"cannot infer freq from a non-convertible index of dtype {index.dtype}"
|
179 | 182 | )
|
180 |
| - # error: Incompatible types in assignment (expression has type |
181 |
| - # "Union[ExtensionArray, ndarray[Any, Any]]", variable has type |
182 |
| - # "Union[DatetimeIndex, TimedeltaIndex, Series, DatetimeLikeArrayMixin]") |
183 |
| - index = index._values # type: ignore[assignment] |
| 183 | + index = cast("TimedeltaArray", index) |
184 | 184 |
|
185 | 185 | if not isinstance(index, DatetimeIndex):
|
186 | 186 | index = DatetimeIndex(index)
|
|
0 commit comments