GH-50464: [C++][Python] Simplify arrow_to_pandas DateOffset handling for nanoseconds/milliseconds - #50465
Conversation
…dling for nanoseconds/milliseconds
|
|
…Year, seconds, microseconds, minutes, hours, weeks
|
@jorisvandenbossche would there be any reason to keep roundtriping microseconds on |
There was a problem hiding this comment.
Pull request overview
This PR simplifies the Pandas DateOffset conversion path for month_day_nano_interval in arrow_to_pandas by no longer splitting nanoseconds into microseconds + remainder nanoseconds, and updates Python tests to expect nanoseconds-only roundtripping.
Changes:
- Simplify
MonthDayNanoIntervalType→ PandasDateOffsetconversion to passinterval.nanosecondsdirectly. - Update interval/dateoffset-related tests to assert nanoseconds-only behavior.
- Adjust expected
DateOffsetconstruction in tests to reflect the new output shape.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/pyarrow/src/arrow/python/arrow_to_pandas.cc | Removes microseconds/remainder splitting and passes total nanoseconds into DateOffset. |
| python/pyarrow/tests/test_pandas.py | Updates roundtrip expectations for DateOffset values produced from month-day-nano intervals. |
| python/pyarrow/tests/test_array.py | Updates DateOffset expectations in interval array tests to use nanoseconds-only representation. |
| PyDict_SetItemString(kwargs.obj(), "months", PyLong_FromLong(interval.months)); | ||
| PyDict_SetItemString(kwargs.obj(), "days", PyLong_FromLong(interval.days)); | ||
| PyDict_SetItemString(kwargs.obj(), "microseconds", | ||
| PyLong_FromLongLong(microseconds)); | ||
| PyDict_SetItemString(kwargs.obj(), "nanoseconds", PyLong_FromLongLong(nanoseconds)); | ||
| PyDict_SetItemString(kwargs.obj(), "nanoseconds", | ||
| PyLong_FromLongLong(interval.nanoseconds)); |
There was a problem hiding this comment.
I think this Copilot comment is right @raulcd , though obviously it already applied to the existing code.
There was a problem hiding this comment.
Thanks @pitrou, applied the fix. I am unsure whether all RETURN_IF_PYERROR checks are necessary or one after all PyDict_SetItemString is enough as I don't think PyErr_Clear is exercised but I suppose better safe than sorry. Is there a way to know which CPython methods from the C-API clear PyErr?
There was a problem hiding this comment.
Normally, C API functions shouldn't clear the error indicator. You should still call RETURN_IF_PYERROR after creating all the OwnedRefs, I think, lest one of them may be NULL.
There was a problem hiding this comment.
I think this is what I've done, have I missed any?
There was a problem hiding this comment.
No. But you need only one RETURN_IF_PYERROR after all the PyDict_SetItemString, not after each one of them.
There was a problem hiding this comment.
ah! I understand you now. Yes the single RETURN_IF_PYERROR after all PyDict_SetItemString is what I wasn't sure. Thanks for confirming.
…r Error on PyDict_SetItemString
|
I am going to merge this. This is currently not only simplifying part of the custom logic but fixing existing reference leaks. |
|
After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit 6287272. There were no benchmark performance regressions. 🎉 The full Conbench report has more details. It also includes information about 3 possible false positives for unstable benchmarks that are known to sometimes produce them. |
Rationale for this change
Pandas 1.4 fixed the following:
Which made us special case microseconds extracting those from our
MonthDayNanoand manually buildingmicroseconds.DateOffsetcan be created with the following:We currently only roundtrip Month, Day, Microseconds and Nano due to our MonthDayNano data type. We special cased microseconds but we were not special casing years, seconds, minutos, hours or weeks.
As the initial issue that made us special case has been solved upstream we can stop roundtripping microseconds and just return nanoseconds simplifying our codebase.
What changes are included in this PR?
Remove the unnecessary code to retrieve microseconds from nanoseconds.
Update test as microseconds won't be returned, only nanoseconds.
Are these changes tested?
Yes via CI
Are there any user-facing changes?
Yes, this is a change of the UX.