Skip to content

Commit a1d990b

Browse files
committed
remove mypy ignore[assignment] and cast index to TimedeltaArray
1 parent d182a34 commit a1d990b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

pandas/tseries/frequencies.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import (
4+
TYPE_CHECKING,
5+
cast,
6+
)
47

58
import numpy as np
69

@@ -51,6 +54,7 @@
5154
Series,
5255
TimedeltaIndex,
5356
)
57+
from pandas.core.arrays import TimedeltaArray
5458
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
5559
# ---------------------------------------------------------------------
5660
# Offset names ("time rules") and related functions
@@ -140,10 +144,7 @@ def infer_freq(
140144
>>> pd.infer_freq(idx)
141145
'D'
142146
"""
143-
from pandas.core.api import (
144-
DatetimeIndex,
145-
Index,
146-
)
147+
from pandas.core.api import DatetimeIndex
147148

148149
if isinstance(index, ABCSeries):
149150
values = index._values
@@ -172,15 +173,14 @@ def infer_freq(
172173
inferer = _TimedeltaFrequencyInferer(index)
173174
return inferer.get_freq()
174175

175-
if isinstance(index, Index) and not isinstance(index, DatetimeIndex):
176+
if not hasattr(index, "dtype"):
177+
pass
178+
elif isinstance(index.dtype, object):
176179
if is_numeric_dtype(index.dtype):
177180
raise TypeError(
178181
f"cannot infer freq from a non-convertible index of dtype {index.dtype}"
179182
)
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)
184184

185185
if not isinstance(index, DatetimeIndex):
186186
index = DatetimeIndex(index)

0 commit comments

Comments
 (0)