Skip to content

Bug: raise TypeError when doing infer_freq using RangeIndex #48497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Categorical

Datetimelike
^^^^^^^^^^^^
-
- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`)
-

Timedelta
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/tseries/frequencies/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,16 @@ 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),
tm.makeRangeIndex(10),
],
)
def test_invalid_index_types(idx):
# see gh-48439
msg = "|".join(
[
"cannot infer freq from a non-convertible",
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def infer_freq(index, warn: bool = True) -> str | None:
Float64Index,
Index,
Int64Index,
RangeIndex,
)

if isinstance(index, ABCSeries):
Expand Down Expand Up @@ -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)}"
)
Expand Down