From ca65534ae057b1d1d3688d595673b2f6874420aa Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Sat, 10 Sep 2022 14:26:32 +0200 Subject: [PATCH 1/5] Fix: raise TypeError when doing infer_freq using RangeIndex --- pandas/tests/tseries/frequencies/test_inference.py | 9 ++++++++- pandas/tseries/frequencies.py | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tseries/frequencies/test_inference.py b/pandas/tests/tseries/frequencies/test_inference.py index 396cb950bd8b2..b34d1f90cf74b 100644 --- a/pandas/tests/tseries/frequencies/test_inference.py +++ b/pandas/tests/tseries/frequencies/test_inference.py @@ -16,6 +16,7 @@ from pandas import ( DatetimeIndex, Index, + RangeIndex, Series, Timestamp, date_range, @@ -358,7 +359,13 @@ def test_non_datetime_index2(): @pytest.mark.parametrize( - "idx", [tm.makeIntIndex(10), tm.makeFloatIndex(10), tm.makePeriodIndex(10)] + "idx", + [ + tm.makeIntIndex(10), + tm.makeFloatIndex(10), + tm.makePeriodIndex(10), + RangeIndex(10), # see gh-48439 + ], ) def test_invalid_index_types(idx): msg = "|".join( diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index 3edabc7c089e1..ce62a15eda367 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -160,6 +160,7 @@ def infer_freq(index, warn: bool = True) -> str | None: Float64Index, Index, Int64Index, + RangeIndex, ) if isinstance(index, ABCSeries): @@ -190,7 +191,7 @@ def infer_freq(index, warn: bool = True) -> str | None: return inferer.get_freq() if isinstance(index, Index) and not isinstance(index, DatetimeIndex): - if isinstance(index, (Int64Index, Float64Index)): + if isinstance(index, (Int64Index, Float64Index, RangeIndex)): raise TypeError( f"cannot infer freq from a non-convertible index type {type(index)}" ) From e1efcc625a56e8dfc627976f2f145e538619785e Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 12 Sep 2022 09:04:26 +0200 Subject: [PATCH 2/5] use tm.makeRangeIndex instead of RangeIndex --- pandas/tests/tseries/frequencies/test_inference.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tseries/frequencies/test_inference.py b/pandas/tests/tseries/frequencies/test_inference.py index b34d1f90cf74b..ea9a09ff2d65c 100644 --- a/pandas/tests/tseries/frequencies/test_inference.py +++ b/pandas/tests/tseries/frequencies/test_inference.py @@ -16,7 +16,6 @@ from pandas import ( DatetimeIndex, Index, - RangeIndex, Series, Timestamp, date_range, @@ -364,10 +363,11 @@ def test_non_datetime_index2(): tm.makeIntIndex(10), tm.makeFloatIndex(10), tm.makePeriodIndex(10), - RangeIndex(10), # see gh-48439 + tm.makeRangeIndex(10), ], ) def test_invalid_index_types(idx): + # see gh-48439 msg = "|".join( [ "cannot infer freq from a non-convertible", From de150394dc81520d6771f2260b6774701405d816 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 12 Sep 2022 09:05:46 +0200 Subject: [PATCH 3/5] add: infer_freq bugfix to whatsnew --- doc/source/whatsnew/v1.6.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index eac5e5d3a0f52..eec20a28293bc 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -206,7 +206,7 @@ Styler Other ^^^^^ - +- Bug in :func: `infer_freq` raising ``TypeError`` when called with `RangeIndex` (:issue: `48439`) .. ***DO NOT USE THIS SECTION*** - From 7dd9c13da621e53703886cabb61fed8dfd54fbce Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 12 Sep 2022 11:03:05 +0200 Subject: [PATCH 4/5] fix pre-commit errors when adding bugfix to whatsnew.rst --- doc/source/whatsnew/v1.6.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index ee111db101db1..e0702eba3456b 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -213,7 +213,8 @@ Styler Other ^^^^^ -- Bug in :func: `infer_freq` raising ``TypeError`` when called with `RangeIndex` (:issue: `48439`) +- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`) + .. ***DO NOT USE THIS SECTION*** - From 7180e01183e62b006eafd2b037135838d7f43370 Mon Sep 17 00:00:00 2001 From: Ambuj Pawar Date: Mon, 12 Sep 2022 23:19:39 +0200 Subject: [PATCH 5/5] Move bugfix from other to datetimelike section --- doc/source/whatsnew/v1.6.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index e0702eba3456b..975909686c28f 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -121,7 +121,7 @@ Categorical Datetimelike ^^^^^^^^^^^^ -- +- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`) - Timedelta @@ -213,7 +213,6 @@ Styler Other ^^^^^ -- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`) .. ***DO NOT USE THIS SECTION***