Skip to content

REF: remove DatetimeArray._with_freq, further freq management cleanup (GH#24566)#65276

Merged
mroeschke merged 5 commits into
pandas-dev:mainfrom
jbrockmendel:ref-array-no-freq-11
Apr 18, 2026
Merged

REF: remove DatetimeArray._with_freq, further freq management cleanup (GH#24566)#65276
mroeschke merged 5 commits into
pandas-dev:mainfrom
jbrockmendel:ref-array-no-freq-11

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Summary

Stacks on top of #65266 and continues the GH#24566 effort to move freq management out of DatetimeArray/TimedeltaArray and onto the Index layer.

  • REF: make DatetimeTimedeltaMixin._with_freq self-contained — Index-level _with_freq stops delegating to the array's _with_freq. It does its own view() + _freq = and handles all validation (None / BaseOffset+Tick-for-m-dtype / "infer" resolution / else ValueError) directly, mirroring the as_unit pattern.
  • REF: stop calling DatetimeLikeArrayMixin._with_freq from non-test core callers — The three non-test core callers (blocks.maybe_coerce_values, PeriodArray.to_timestamp, DatetimeIndex.normalize) now manage _freq directly via view() + _freq = or to_offset(inferred_freq).
  • REF: remove DatetimeLikeArrayMixin._with_freq — With no remaining non-test callers, the array method is deleted (~29 lines). ~10 test callers updated to direct _freq = assignment. The _union fallback now wraps super()._union via _wrap_setop_result before calling Index-level _with_freq, since the base _union can return an ArrayLike rather than an Index.
  • CLN: drop redundant freq=None from DTA/TDA _simple_new call sites — 7 call sites where freq=None was redundant noise (it's already the default), plus several now-obsolete # type: ignore[call-arg] comments.
  • TST: update test_to_datetime_dta_tz for array-level _with_freq removal — One missed test caller.

Test plan

  • pytest pandas/tests/arithmetic/ pandas/tests/resample/ pandas/tests/series/ pandas/tests/arrays/ pandas/tests/indexes/ pandas/tests/tseries/ pandas/tests/frame/ pandas/tests/internals/ pandas/tests/extension/ pandas/tests/tools/test_to_datetime.py — all pass
  • mypy pandas/core/arrays/datetimelike.py pandas/core/indexes/datetimelike.py pandas/core/arrays/datetimes.py pandas/core/arrays/timedeltas.py — no new errors
  • pre-commit hooks pass

🤖 Generated with Claude Code

jbrockmendel and others added 5 commits April 18, 2026 10:07
Index-level _with_freq no longer delegates to the array's _with_freq.
It now performs the view + _freq assign directly, mirroring the
as_unit pattern, and handles all validation (None, BaseOffset with
m-dtype Tick check, 'infer' resolution, else ValueError) itself.

Another step toward GH#24566: freq management continues to migrate
from DTA/TDA up to the Index layer.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…e callers

Three non-test callers go through the array's _with_freq helper:

- blocks.maybe_coerce_values strips freq when wrapping into a Block
- PeriodArray.to_timestamp assigns inferred freq on a fresh DTA
- DatetimeIndex.normalize assigns inferred freq on a fresh DTA

Replace each with direct `view()` + `_freq =` / `_freq =` assignment,
which skips the array-level validation (all three pass fixed values so
validation is a no-op) and leaves array._with_freq closer to being
removable in a later step.

Another step toward GH#24566.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
No core callers remain after the previous refactor. The ~10 test
callers of array-level _with_freq are updated to use direct
`_freq =` assignment (or `.view(); _freq =`) or `to_offset(inferred_freq)`.

The `_union` fallback in DatetimeTimedeltaMixin now wraps the
`super()._union` result via `_wrap_setop_result` before calling
the Index-level `_with_freq`, since the base `_union` can return
an ArrayLike rather than an Index.

Removes ~29 lines from datetimelike.py array mixin.

Another step toward GH#24566.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
freq defaults to None in both DatetimeArray._simple_new and
TimedeltaArray._simple_new, so `freq=None` is redundant noise.
Also removes several now-unnecessary type-ignore comments.

Another step toward GH#24566.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Missed caller from the _with_freq removal sweep; switch to direct
`_freq =` assignment to match the other array callers.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel added the Refactor Internal refactoring of code label Apr 18, 2026
@mroeschke mroeschke added this to the 3.1 milestone Apr 18, 2026
@mroeschke mroeschke merged commit 093c0ad into pandas-dev:main Apr 18, 2026
84 of 85 checks passed
@mroeschke

Copy link
Copy Markdown
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the ref-array-no-freq-11 branch April 18, 2026 21:36
jbrockmendel added a commit to jbrockmendel/pandas that referenced this pull request Apr 19, 2026
…566)

In-place arithmetic on a DTA/TDA used to sync `_freq` from the
arithmetic result. That belonged to the array's freq-management era;
under the current design `_freq` is Index-managed dumb storage on the
array. Mutating values via __iadd__/__isub__ leaves _freq alone (it can
go stale), matching __setitem__'s behaviour after pandas-dev#65276's cleanup.

Tests that compared `dta += x` vs `dta + x` for the pd.array boxed case
now sync the expected freq from the iadd-result before comparison,
since the rebound `dta + x` returns a freq-stripped array while the
in-place version preserves the array's pre-iadd freq.
hamdanal pushed a commit to hamdanal/pandas that referenced this pull request Jun 27, 2026
… (GH#24566) (pandas-dev#65276)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Refactor Internal refactoring of code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants