Skip to content
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ Datetimelike
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
- Bug in :func:`infer_freq` with a :class:`Series` with :class:`ArrowDtype` timestamp dtype incorrectly raising ``TypeError`` (:issue:`58403`)
- Bug in :func:`to_datetime` where passing an ``lxml.etree._ElementUnicodeResult`` together with ``format`` raised ``TypeError``. Now subclasses of ``str`` are handled. (:issue:`60933`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
- Bug in :func:`tseries.frequencies.to_offset` would fail to parse frequency strings starting with "LWOM" (:issue:`59218`)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/tseries/frequencies/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas._libs.tslibs.offsets import _get_offset
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
from pandas.compat import is_platform_windows
import pandas.util._test_decorators as td

from pandas import (
DatetimeIndex,
Expand Down Expand Up @@ -542,3 +543,16 @@ def test_infer_freq_non_nano_tzaware(tz_aware_fixture):

res = frequencies.infer_freq(dta)
assert res == "B"


@td.skip_if_no("pyarrow")
def test_infer_freq_pyarrow():
# GH#58403
data = ["2022-01-01T10:00:00", "2022-01-01T10:00:30", "2022-01-01T10:01:00"]
pd_series = Series(data).astype("timestamp[s][pyarrow]")
pd_index = Index(data).astype("timestamp[s][pyarrow]")

assert frequencies.infer_freq(pd_index.values) == "30s"
assert frequencies.infer_freq(pd_series.values) == "30s"
assert frequencies.infer_freq(pd_index) == "30s"
assert frequencies.infer_freq(pd_series) == "30s"
9 changes: 9 additions & 0 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

from pandas.core.dtypes.common import is_numeric_dtype
from pandas.core.dtypes.dtypes import (
ArrowDtype,
DatetimeTZDtype,
PeriodDtype,
)
Expand Down Expand Up @@ -132,6 +133,14 @@ def infer_freq(

if isinstance(index, ABCSeries):
values = index._values

if isinstance(index.dtype, ArrowDtype):
import pyarrow as pa

if pa.types.is_timestamp(values.dtype.pyarrow_dtype):
# GH#58403
values = values._to_datetimearray()

if not (
lib.is_np_dtype(values.dtype, "mM")
or isinstance(values.dtype, DatetimeTZDtype)
Expand Down
Loading