Skip to content

Commit 9be7f06

Browse files
authored
BUG: infer_freq with Series[pyarrow[timestamp]] (#62942)
1 parent 8226043 commit 9be7f06

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ Datetimelike
975975
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
976976
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
977977
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
978+
- Bug in :func:`infer_freq` with a :class:`Series` with :class:`ArrowDtype` timestamp dtype incorrectly raising ``TypeError`` (:issue:`58403`)
978979
- Bug in :func:`to_datetime` where passing an ``lxml.etree._ElementUnicodeResult`` together with ``format`` raised ``TypeError``. Now subclasses of ``str`` are handled. (:issue:`60933`)
979980
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
980981
- Bug in :func:`tseries.frequencies.to_offset` would fail to parse frequency strings starting with "LWOM" (:issue:`59218`)

pandas/tests/tseries/frequencies/test_inference.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas._libs.tslibs.offsets import _get_offset
1414
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
1515
from pandas.compat import is_platform_windows
16+
import pandas.util._test_decorators as td
1617

1718
from pandas import (
1819
DatetimeIndex,
@@ -542,3 +543,16 @@ def test_infer_freq_non_nano_tzaware(tz_aware_fixture):
542543

543544
res = frequencies.infer_freq(dta)
544545
assert res == "B"
546+
547+
548+
@td.skip_if_no("pyarrow")
549+
def test_infer_freq_pyarrow():
550+
# GH#58403
551+
data = ["2022-01-01T10:00:00", "2022-01-01T10:00:30", "2022-01-01T10:01:00"]
552+
pd_series = Series(data).astype("timestamp[s][pyarrow]")
553+
pd_index = Index(data).astype("timestamp[s][pyarrow]")
554+
555+
assert frequencies.infer_freq(pd_index.values) == "30s"
556+
assert frequencies.infer_freq(pd_series.values) == "30s"
557+
assert frequencies.infer_freq(pd_index) == "30s"
558+
assert frequencies.infer_freq(pd_series) == "30s"

pandas/tseries/frequencies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
from pandas.core.dtypes.common import is_numeric_dtype
3939
from pandas.core.dtypes.dtypes import (
40+
ArrowDtype,
4041
DatetimeTZDtype,
4142
PeriodDtype,
4243
)
@@ -132,6 +133,14 @@ def infer_freq(
132133

133134
if isinstance(index, ABCSeries):
134135
values = index._values
136+
137+
if isinstance(index.dtype, ArrowDtype):
138+
import pyarrow as pa
139+
140+
if pa.types.is_timestamp(values.dtype.pyarrow_dtype):
141+
# GH#58403
142+
values = values._to_datetimearray()
143+
135144
if not (
136145
lib.is_np_dtype(values.dtype, "mM")
137146
or isinstance(values.dtype, DatetimeTZDtype)

0 commit comments

Comments
 (0)