From a1d990b6b25b0f351418aa99868672a082133cb6 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 26 Apr 2023 00:06:24 +0200 Subject: [PATCH 1/3] remove mypy ignore[assignment] and cast index to TimedeltaArray --- pandas/tseries/frequencies.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 522daf7188bfb..391d076acf68f 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -1,6 +1,9 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from typing import ( + TYPE_CHECKING, + cast, +) import numpy as np @@ -51,6 +54,7 @@ Series, TimedeltaIndex, ) + from pandas.core.arrays import TimedeltaArray from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin # --------------------------------------------------------------------- # Offset names ("time rules") and related functions @@ -140,10 +144,7 @@ def infer_freq( >>> pd.infer_freq(idx) 'D' """ - from pandas.core.api import ( - DatetimeIndex, - Index, - ) + from pandas.core.api import DatetimeIndex if isinstance(index, ABCSeries): values = index._values @@ -172,15 +173,14 @@ def infer_freq( inferer = _TimedeltaFrequencyInferer(index) return inferer.get_freq() - if isinstance(index, Index) and not isinstance(index, DatetimeIndex): + if not hasattr(index, "dtype"): + pass + elif isinstance(index.dtype, object): if is_numeric_dtype(index.dtype): raise TypeError( f"cannot infer freq from a non-convertible index of dtype {index.dtype}" ) - # error: Incompatible types in assignment (expression has type - # "Union[ExtensionArray, ndarray[Any, Any]]", variable has type - # "Union[DatetimeIndex, TimedeltaIndex, Series, DatetimeLikeArrayMixin]") - index = index._values # type: ignore[assignment] + index = cast("TimedeltaArray", index) if not isinstance(index, DatetimeIndex): index = DatetimeIndex(index) From 22b8337e3353f30d5fe61354365f8822f8335759 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sun, 7 May 2023 14:14:00 +0200 Subject: [PATCH 2/3] remove check index.dtype is an object --- pandas/tseries/frequencies.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 391d076acf68f..f57013c30a3f4 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -144,7 +144,10 @@ def infer_freq( >>> pd.infer_freq(idx) 'D' """ - from pandas.core.api import DatetimeIndex + from pandas.core.api import ( + DatetimeIndex, + Index, + ) if isinstance(index, ABCSeries): values = index._values @@ -173,14 +176,12 @@ def infer_freq( inferer = _TimedeltaFrequencyInferer(index) return inferer.get_freq() - if not hasattr(index, "dtype"): - pass - elif isinstance(index.dtype, object): + elif isinstance(index, Index): if is_numeric_dtype(index.dtype): raise TypeError( f"cannot infer freq from a non-convertible index of dtype {index.dtype}" ) - index = cast("TimedeltaArray", index) + index = cast("TimedeltaArray", index._values) if not isinstance(index, DatetimeIndex): index = DatetimeIndex(index) From 1e832435619f1682fe4dc0de40c7e3c9c2352b7a Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Sun, 7 May 2023 19:32:28 +0200 Subject: [PATCH 3/3] remove cast to TimedeltaArray --- pandas/tseries/frequencies.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index f57013c30a3f4..caa34a067ac69 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -1,9 +1,6 @@ from __future__ import annotations -from typing import ( - TYPE_CHECKING, - cast, -) +from typing import TYPE_CHECKING import numpy as np @@ -54,7 +51,6 @@ Series, TimedeltaIndex, ) - from pandas.core.arrays import TimedeltaArray from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin # --------------------------------------------------------------------- # Offset names ("time rules") and related functions @@ -144,10 +140,7 @@ def infer_freq( >>> pd.infer_freq(idx) 'D' """ - from pandas.core.api import ( - DatetimeIndex, - Index, - ) + from pandas.core.api import DatetimeIndex if isinstance(index, ABCSeries): values = index._values @@ -176,12 +169,10 @@ def infer_freq( inferer = _TimedeltaFrequencyInferer(index) return inferer.get_freq() - elif isinstance(index, Index): - if is_numeric_dtype(index.dtype): - raise TypeError( - f"cannot infer freq from a non-convertible index of dtype {index.dtype}" - ) - index = cast("TimedeltaArray", index._values) + elif is_numeric_dtype(index.dtype): + raise TypeError( + f"cannot infer freq from a non-convertible index of dtype {index.dtype}" + ) if not isinstance(index, DatetimeIndex): index = DatetimeIndex(index)