Skip to content

Make infer_freq accept Series of timezone-aware Timestamps #52528

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 2 commits into from
Apr 10, 2023
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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Timedelta

Timezones
^^^^^^^^^
-
- Bug in :func:`infer_freq` that raises ``TypeError`` for ``Series`` of timezone-aware timestamps (:issue:`52456`)
-

Numeric
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tseries/frequencies/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ def test_infer_freq_tz(tz_naive_fixture, expected, dates):
assert idx.inferred_freq == expected


def test_infer_freq_tz_series(tz_naive_fixture):
# infer_freq should work with both tz-naive and tz-aware series. See gh-52456
tz = tz_naive_fixture
idx = date_range("2021-01-01", "2021-01-04", tz=tz)
series = idx.to_series().reset_index(drop=True)
inferred_freq = frequencies.infer_freq(series)
assert inferred_freq == "D"


@pytest.mark.parametrize(
"date_pair",
[
Expand Down
4 changes: 3 additions & 1 deletion pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_datetime64tz_dtype,
is_numeric_dtype,
is_timedelta64_dtype,
)
Expand Down Expand Up @@ -120,7 +121,7 @@ def infer_freq(

Parameters
----------
index : DatetimeIndex or TimedeltaIndex
index : DatetimeIndex, TimedeltaIndex, Series or array-like
If passed a Series will use the values of the series (NOT THE INDEX).

Returns
Expand Down Expand Up @@ -150,6 +151,7 @@ def infer_freq(
values = index._values
if not (
is_datetime64_dtype(values)
or is_datetime64tz_dtype(values)
or is_timedelta64_dtype(values)
or values.dtype == object
):
Expand Down