Skip to content

GH-29828: [Python] Support other interval types - #55811

Open
AlenkaF wants to merge 5 commits into
apache:mainfrom
AlenkaF:gh-29828-interval-types
Open

AlenkaF wants to merge 5 commits into
apache:mainfrom
AlenkaF:gh-29828-interval-types

Conversation

@AlenkaF

@AlenkaF AlenkaF commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

month_day_nano_interval is supported by PyArrow but other interval types are missing (day_time_interval and month_interval).

What changes are included in this PR?

The changes made by Joris in jorisvandenbossche@97a0953:

  • month_interval() and day_time_interval() factories
  • pa.types.is_interval for all three interval types
  • array and scalar classes
  • API docs updates

Picking up where Joris has left off I added:

  • as_py() on both scalar classes,
  • doctrings on the types constructors.

Current decisions:

  • MonthIntervalScalar.as_py() returns int (number of months)
  • DayTimeIntervalScalar.as_py() returns plain tuple (days, milliseconds) with the raw values.

A possible follow-up (or a change in this PR if preferred) would be a named tuple, e.g. pa.DayTime(days, milliseconds).
Planned follow-ups:

  • pa.scalar, pa.array from Python values
  • to_numpy(), to_pandas(): returning object dtype with the as_py() values (or timedelta64[M] for months)

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Pickle round-trips for both new scalar classes raise ArrowNotImplementedError, including for nulls, because the reducer uses unsupported Python-value conversion.

1 open issue
What changed in this PR

Extends PyArrow’s interval support with month and day-time types, allowing existing compute functions to return typed Python arrays and scalars.

Changes:

  • Adds public type factories, aliases, and interval classification.
  • Adds array and scalar wrappers, with as_py() returning month counts or (days, milliseconds) tuples.
  • Expands tests and API documentation.
File Description
python/​pyarrow/​types.py Recognizes all three interval types.
python/​pyarrow/​types.pxi Adds factories, aliases, and usage examples.
python/​pyarrow/​tests/​test_types.py Expands interval type coverage.
python/​pyarrow/​tests/​test_scalars.py Tests scalar values and null conversion.
python/​pyarrow/​tests/​test_misc.py Adds interval classes to shared checks.
python/​pyarrow/​tests/​test_compute.py Tests interval compute-result wrappers and values.
python/​pyarrow/​src/​arrow/​python/​helpers.cc Extends primitive type lookup.
python/​pyarrow/​scalar.pxi Implements and registers the new scalar classes.
python/​pyarrow/​lib.pyx Groups interval type constants together.
python/​pyarrow/​lib.pxd Declares the new array classes.
python/​pyarrow/​includes/​libarrow.pxd Adds C++ interval scalar bindings.
python/​pyarrow/​array.pxi Implements and registers the new array classes.
python/​pyarrow/​__init__.py Exports the new public APIs.
docs/​source/​python/​api/​datatypes.rst Lists the new type factories.
docs/​source/​python/​api/​arrays.rst Lists the new array and scalar classes.

Review effort: Balanced

Comment thread python/pyarrow/scalar.pxi
return datetime.timedelta(microseconds=sp.value // 1000)


cdef class MonthIntervalScalar(Scalar):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, current __reduce__ needs the Python-to-Arrow conversion which I am workin on next (follow-up PR). We can add the change proposed here or wait for the follow-up PR where the round-trip will not raise an error and we can add a test there.

Copilot AI balanced review requested due to automatic review settings November 27, 2026 10:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Both new interval scalar classes use an unsupported Python-value reconstruction path, causing pickle round-trips and shallow/deep copies to raise ArrowNotImplementedError even for null scalars.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Comment thread python/pyarrow/scalar.pxi
if not scalar.is_valid:
return None

return (scalar.value.days, scalar.value.milliseconds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if a NamedTuple or dataclass would be better here for explicitness

Comment thread python/pyarrow/scalar.pxi
if not scalar.is_valid:
return None

return scalar.value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could also be a namedtuple, perhaps, or maybe that's excessive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] Support other interval types

4 participants