Skip to content
Open
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
17 changes: 2 additions & 15 deletions python/pyarrow/src/arrow/python/arrow_to_pandas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1292,24 +1292,11 @@ struct ObjectWriterVisitor {
auto to_date_offset = [&](const MonthDayNanoIntervalType::MonthDayNanos& interval,
PyObject** out) {
ARROW_DCHECK(internal::BorrowPandasDataOffsetType() != nullptr);
// DateOffset objects do not add nanoseconds component to pd.Timestamp.
// as of Pandas 1.3.3
// (https://github.com/pandas-dev/pandas/issues/43892).
// So convert microseconds and remainder to preserve data
// but give users more expected results.
int64_t microseconds = interval.nanoseconds / 1000;
int64_t nanoseconds;
if (interval.nanoseconds >= 0) {
nanoseconds = interval.nanoseconds % 1000;
} else {
nanoseconds = -((-interval.nanoseconds) % 1000);
}

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));
Comment on lines 1296 to +1299
*out =
PyObject_Call(internal::BorrowPandasDataOffsetType(), args.obj(), kwargs.obj());
RETURN_IF_PYERROR();
Expand Down
12 changes: 5 additions & 7 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2762,11 +2762,10 @@ def test_interval_array_from_relativedelta():
assert arr.equals(expected)
assert arr.to_pandas().tolist() == [
None, DateOffset(months=13, days=8,
microseconds=(
nanoseconds=(
datetime.timedelta(seconds=1, microseconds=1,
minutes=1, hours=1) //
datetime.timedelta(microseconds=1)),
nanoseconds=0)]
datetime.timedelta(microseconds=1)) * 1000)]
with pytest.raises(ValueError):
pa.array([DateOffset(years=((1 << 32) // 12), months=100)])
with pytest.raises(ValueError):
Expand Down Expand Up @@ -2814,12 +2813,11 @@ def test_interval_array_from_dateoffset():
assert arr.equals(expected)
expected_from_pandas = [
None, DateOffset(months=13, days=8,
microseconds=(
nanoseconds=(
datetime.timedelta(seconds=1, microseconds=1,
minutes=1, hours=1) //
datetime.timedelta(microseconds=1)),
nanoseconds=1),
DateOffset(months=0, days=0, microseconds=0, nanoseconds=0)]
datetime.timedelta(microseconds=1) * 1000) + 1),
DateOffset(months=0, days=0, nanoseconds=0)]

assert arr.to_pandas().tolist() == expected_from_pandas

Expand Down
3 changes: 1 addition & 2 deletions python/pyarrow/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,8 +1733,7 @@ def test_month_day_nano_interval(self):
from pandas.tseries.offsets import DateOffset
df = pd.DataFrame({
'date_offset': [None,
DateOffset(days=3600, months=3600, microseconds=3,
nanoseconds=600)]
DateOffset(days=3600, months=3600, nanoseconds=3600)]
})
schema = pa.schema([('date_offset', pa.month_day_nano_interval())])
_check_pandas_roundtrip(
Expand Down
Loading