This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git
The following commit(s) were added to refs/heads/main by this push:
new a392a627 feat(python): Allow passing an iterable of `datetime.date`
obejcts to `Array(iterable, <date32 or date64>)` by converting the values to
the appropriate integer representation (#1027)
a392a627 is described below
commit a392a6276b027669797654ff7d5a866a90ba46e3
Author: Edgar Ramírez Mondragón
<[email protected]>
AuthorDate: Sat Nov 21 21:26:16 2026 -0600
feat(python): Allow passing an iterable of `datetime.date` obejcts to
`Array(iterable, <date32 or date64>)` by converting the values to the
appropriate integer representation (#1027)
Relates to https://github.com/apache/arrow-nanoarrow/issues/478.
Specifically,
https://github.com/apache/arrow-nanoarrow/issues/478#issuecomment-2119492920
saying a method could be added to `ArrayFromIterableBuilder` doing the
inverse of `PyIterator._date_iter`:
https://github.com/apache/arrow-nanoarrow/blob/6fc647a48478dcd10c63313feb9f3e651a873f9a/python/src/nanoarrow/iterator.py#L448-L465
Signed-off-by: Edgar Ramírez-Mondragón <[email protected]>
---
python/src/nanoarrow/c_array.py | 32 ++++++++++++++++++++++++++++++++
python/tests/test_array.py | 18 ++++++++++++++++++
2 files changed, 50 insertions(+)
diff --git a/python/src/nanoarrow/c_array.py b/python/src/nanoarrow/c_array.py
index e2131bf4..365ed18f 100644
--- a/python/src/nanoarrow/c_array.py
+++ b/python/src/nanoarrow/c_array.py
@@ -512,6 +512,15 @@ class ArrayFromIterableBuilder(ArrayBuilder):
self._append_impl = getattr(self, method_name)
+ # Some types accept Python objects (e.g., datetime.date) that must be
+ # converted to their storage integer before being appended. Integers
+ # and None are passed through unchanged.
+ converter_name = _ARRAY_BUILDER_FROM_ITERABLE_CONVERTER.get(type_id)
+ if converter_name is not None:
+ convert = getattr(self, converter_name)
+ append = self._append_impl
+ self._append_impl = lambda obj: append(convert(obj))
+
def start_building(self) -> None:
self._c_builder.start_appending()
@@ -558,6 +567,24 @@ class ArrayFromIterableBuilder(ArrayBuilder):
self._c_builder.set_null_count(0)
self._c_builder.set_offset(0)
+ def _convert_dates(self, obj: Iterable) -> Iterable:
+ from datetime import date, datetime
+
+ epoch_ordinal = date(1970, 1, 1).toordinal()
+ scale = 1 if self._schema_view.type_id == _types.DATE32 else 86_400_000
+
+ for item in obj:
+ if isinstance(item, datetime):
+ msg = (
+ f"Can't convert datetime.datetime to
{self._schema_view.type}; "
+ "use .date() to convert it to a datetime.date first"
+ )
+ raise TypeError(msg)
+ elif isinstance(item, date):
+ yield (item.toordinal() - epoch_ordinal) * scale
+ else:
+ yield item
+
def _append_using_buffer_builder(self, obj: Iterable) -> None:
builder = CBufferBuilder()
builder.set_data_type(self._schema_view.type_id)
@@ -600,6 +627,11 @@ _ARRAY_BUILDER_FROM_ITERABLE_METHOD = {
_types.DURATION: "_append_using_array",
}
+_ARRAY_BUILDER_FROM_ITERABLE_CONVERTER = {
+ _types.DATE32: "_convert_dates",
+ _types.DATE64: "_convert_dates",
+}
+
_ARRAY_BUILDER_FROM_NULLABLE_ITERABLE_METHOD = {
"_append_using_array": "_build_nullable_array_using_array",
"_append_using_buffer_builder":
"_build_nullable_array_using_buffer_builder",
diff --git a/python/tests/test_array.py b/python/tests/test_array.py
index 4baae94e..2eddee17 100644
--- a/python/tests/test_array.py
+++ b/python/tests/test_array.py
@@ -408,6 +408,24 @@ def test_date64_array():
assert array.to_pylist() == [d1, d2]
[email protected]("type_", [na.date32(), na.date64()], ids=["date32",
"date64"])
+def test_date_array_from_date_objects(type_):
+ dates = [date(1970, 1, 1), date(1969, 12, 31), None, date(2005, 3, 4)]
+ array = na.Array(dates, type_)
+ assert array.to_pylist() == dates
+
+ # Integers in storage units are still accepted, also mixed with dates
+ one_day = 1 if type_.type == na.Type.DATE32 else 86_400_000
+ array = na.Array([one_day, date(1970, 1, 3)], type_)
+ assert array.to_pylist() == [date(1970, 1, 2), date(1970, 1, 3)]
+
+
[email protected]("type_", [na.date32(), na.date64()], ids=["date32",
"date64"])
+def test_date_array_from_datetime_objects_errors(type_):
+ with pytest.raises(TypeError, match="use .date()"):
+ na.Array([datetime(1970, 1, 1)], type_)
+
+
def test_duration_array():
unix_epoch = date(1970, 1, 1)
d1, d2 = date(1970, 1, 2), date(1970, 1, 3)