Skip to content

Commit 170e689

Browse files
cai-lwmroeschke
andauthored
Make infer_freq accept Series of timezone-aware Timestamps (#52528)
* Make `infer_freq` accept `Series` of timezone-aware timestamps * Update pandas/tests/tseries/frequencies/test_inference.py --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent ba15847 commit 170e689

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ Timedelta
283283

284284
Timezones
285285
^^^^^^^^^
286-
-
286+
- Bug in :func:`infer_freq` that raises ``TypeError`` for ``Series`` of timezone-aware timestamps (:issue:`52456`)
287287
-
288288

289289
Numeric

pandas/tests/tseries/frequencies/test_inference.py

+9
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,15 @@ def test_infer_freq_tz(tz_naive_fixture, expected, dates):
236236
assert idx.inferred_freq == expected
237237

238238

239+
def test_infer_freq_tz_series(tz_naive_fixture):
240+
# infer_freq should work with both tz-naive and tz-aware series. See gh-52456
241+
tz = tz_naive_fixture
242+
idx = date_range("2021-01-01", "2021-01-04", tz=tz)
243+
series = idx.to_series().reset_index(drop=True)
244+
inferred_freq = frequencies.infer_freq(series)
245+
assert inferred_freq == "D"
246+
247+
239248
@pytest.mark.parametrize(
240249
"date_pair",
241250
[

pandas/tseries/frequencies.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from pandas.core.dtypes.common import (
3434
is_datetime64_dtype,
35+
is_datetime64tz_dtype,
3536
is_numeric_dtype,
3637
is_timedelta64_dtype,
3738
)
@@ -120,7 +121,7 @@ def infer_freq(
120121
121122
Parameters
122123
----------
123-
index : DatetimeIndex or TimedeltaIndex
124+
index : DatetimeIndex, TimedeltaIndex, Series or array-like
124125
If passed a Series will use the values of the series (NOT THE INDEX).
125126
126127
Returns
@@ -150,6 +151,7 @@ def infer_freq(
150151
values = index._values
151152
if not (
152153
is_datetime64_dtype(values)
154+
or is_datetime64tz_dtype(values)
153155
or is_timedelta64_dtype(values)
154156
or values.dtype == object
155157
):

0 commit comments

Comments
 (0)