nd-arrow-array 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nd_arrow_array-0.5.0/PKG-INFO +196 -0
- nd_arrow_array-0.5.0/README.md +166 -0
- nd_arrow_array-0.5.0/nd-arrow-array/Cargo.toml +36 -0
- nd_arrow_array-0.5.0/nd-arrow-array/LICENSE +201 -0
- nd_arrow_array-0.5.0/nd-arrow-array/README.md +116 -0
- nd_arrow_array-0.5.0/nd-arrow-array/benches/kernels.rs +56 -0
- nd_arrow_array-0.5.0/nd-arrow-array/examples/basic.rs +22 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/array.rs +169 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/attributes.rs +322 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/batch.rs +438 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/column.rs +626 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/aggregate.rs +193 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/batch.rs +464 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/cf.rs +287 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/column.rs +429 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/elementwise.rs +831 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/groupby.rs +112 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/histogram.rs +156 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/interp.rs +248 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/layout.rs +264 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/methods.rs +117 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/mod.rs +54 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/ops.rs +73 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/partial.rs +349 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/reduce.rs +806 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/resample.rs +430 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/search.rs +328 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/shape.rs +719 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/time.rs +395 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/compute/window.rs +125 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/dimensions.rs +291 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/display.rs +151 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/error.rs +52 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/extension.rs +434 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/lib.rs +68 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/ndarray_interop.rs +80 -0
- nd_arrow_array-0.5.0/nd-arrow-array/src/tensor.rs +432 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/fixtures/gen_numpy.py +172 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/fixtures/numpy.json +18339 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/ipc.rs +117 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/numpy.rs +170 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/properties.rs +125 -0
- nd_arrow_array-0.5.0/nd-arrow-array/tests/python_fixture.rs +55 -0
- nd_arrow_array-0.5.0/nd-arrow-py/Cargo.lock +1031 -0
- nd_arrow_array-0.5.0/nd-arrow-py/Cargo.toml +21 -0
- nd_arrow_array-0.5.0/nd-arrow-py/README.md +166 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/array.rs +225 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/arrow_conv.rs +150 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/attrs.rs +184 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/batch.rs +249 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/column.rs +90 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/error.rs +39 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/lib.rs +24 -0
- nd_arrow_array-0.5.0/nd-arrow-py/src/numpy_conv.rs +340 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_array.py +81 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_arrow_column.py +102 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_attrs.py +61 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_batch.py +116 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_column.py +48 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_import.py +8 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_ipc.py +51 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_numpy.py +95 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/test_xarray.py +226 -0
- nd_arrow_array-0.5.0/nd-arrow-py/tests/typing/check_usage.py +44 -0
- nd_arrow_array-0.5.0/pyproject.toml +37 -0
- nd_arrow_array-0.5.0/python/nd_arrow_array/__init__.py +14 -0
- nd_arrow_array-0.5.0/python/nd_arrow_array/_core.pyi +148 -0
- nd_arrow_array-0.5.0/python/nd_arrow_array/_xarray.py +220 -0
- nd_arrow_array-0.5.0/python/nd_arrow_array/py.typed +0 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nd-arrow-array
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: Programming Language :: Rust
|
|
7
|
+
Classifier: Topic :: Scientific/Engineering
|
|
8
|
+
Classifier: Typing :: Typed
|
|
9
|
+
Requires-Dist: numpy>=1.21
|
|
10
|
+
Requires-Dist: pyarrow>=14 ; extra == 'arrow'
|
|
11
|
+
Requires-Dist: pytest ; extra == 'test'
|
|
12
|
+
Requires-Dist: pyarrow>=14 ; extra == 'test'
|
|
13
|
+
Requires-Dist: xarray>=2023.1 ; extra == 'test'
|
|
14
|
+
Requires-Dist: cftime>=1.6 ; extra == 'test'
|
|
15
|
+
Requires-Dist: mypy ; extra == 'test'
|
|
16
|
+
Requires-Dist: xarray>=2023.1 ; extra == 'xarray'
|
|
17
|
+
Requires-Dist: cftime>=1.6 ; extra == 'xarray'
|
|
18
|
+
Provides-Extra: arrow
|
|
19
|
+
Provides-Extra: test
|
|
20
|
+
Provides-Extra: xarray
|
|
21
|
+
Summary: N-dimensional Arrow arrays with named dimensions and attributes
|
|
22
|
+
Keywords: arrow,ndarray,xarray,netcdf,tensor
|
|
23
|
+
License: Apache-2.0
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
26
|
+
Project-URL: Changelog, https://github.com/robinskil/nd-arrow-array/blob/main/CHANGELOG.md
|
|
27
|
+
Project-URL: Format, https://github.com/robinskil/nd-arrow-array/blob/main/FORMAT.md
|
|
28
|
+
Project-URL: Repository, https://github.com/robinskil/nd-arrow-array
|
|
29
|
+
|
|
30
|
+
# nd-arrow-array (Python)
|
|
31
|
+
|
|
32
|
+
Python bindings for the `nd_arrow.array` Arrow extension type.
|
|
33
|
+
The PyPI package is `nd-arrow-array`. The import name is `nd_arrow_array`.
|
|
34
|
+
The bindings move N-dimensional arrays between NumPy, pyarrow and the
|
|
35
|
+
format in [FORMAT.md](https://github.com/robinskil/nd-arrow-array/blob/main/FORMAT.md).
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install nd-arrow-array # NumPy only
|
|
41
|
+
pip install nd-arrow-array[arrow] # with pyarrow
|
|
42
|
+
pip install nd-arrow-array[xarray] # with xarray and cftime
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
To build from source, run `pip install maturin` and `maturin develop` in this directory.
|
|
46
|
+
|
|
47
|
+
## Classes
|
|
48
|
+
|
|
49
|
+
| Class | Content |
|
|
50
|
+
|---|---|
|
|
51
|
+
| `NdArray` | One array: values, named dimensions, attributes. |
|
|
52
|
+
| `NdColumn` | An Arrow column. Each row is one `NdArray` or `None`. |
|
|
53
|
+
| `NdRecordBatch` | Named arrays on one shared grid, with global attributes. |
|
|
54
|
+
|
|
55
|
+
## Example
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import numpy as np
|
|
59
|
+
import nd_arrow_array as nda
|
|
60
|
+
|
|
61
|
+
a = nda.NdArray.from_numpy(
|
|
62
|
+
np.arange(6.0).reshape(2, 3),
|
|
63
|
+
dims=["time", "lat"],
|
|
64
|
+
attrs={"units": "K", "valid_range": np.array([0, 400], dtype="float32")},
|
|
65
|
+
)
|
|
66
|
+
a.sizes # {'time': 2, 'lat': 3}
|
|
67
|
+
a.set_attr("long_name", "sea surface temperature")
|
|
68
|
+
a.to_numpy()
|
|
69
|
+
|
|
70
|
+
col = nda.NdColumn.from_arrays([a, None])
|
|
71
|
+
arr = col.to_arrow() # pyarrow array
|
|
72
|
+
nda.NdColumn.from_arrow(arr)[0] == a # True
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## pyarrow
|
|
76
|
+
|
|
77
|
+
`pip install nd-arrow-array[arrow]` adds pyarrow. pyarrow sees an nd column as a
|
|
78
|
+
`struct` column. The field metadata `ARROW:extension:name = nd_arrow.array`
|
|
79
|
+
marks it as an nd column. See [FORMAT.md](https://github.com/robinskil/nd-arrow-array/blob/main/FORMAT.md).
|
|
80
|
+
|
|
81
|
+
### Arrays and columns
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
import pyarrow as pa
|
|
85
|
+
|
|
86
|
+
# An array from flat pyarrow values in C order.
|
|
87
|
+
a = nda.NdArray(pa.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]),
|
|
88
|
+
dims={"time": 2, "lat": 3}, attrs={"units": "K"})
|
|
89
|
+
a.values # the flat values as a pyarrow.Array
|
|
90
|
+
|
|
91
|
+
col = nda.NdColumn.from_arrays([a, None])
|
|
92
|
+
arr = col.to_arrow() # pyarrow.StructArray, one row per array
|
|
93
|
+
nda.NdColumn.from_arrow(arr) # back to an NdColumn
|
|
94
|
+
pa.field(col).metadata # the extension name and version
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Record batches and tables
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
lat = nda.NdArray.from_numpy(np.array([-10.0, 0.0, 10.0]), dims=["lat"])
|
|
101
|
+
batch = nda.NdRecordBatch({"sst": a, "lat": lat}, attrs={"title": "demo"})
|
|
102
|
+
|
|
103
|
+
rb = batch.to_arrow() # pyarrow.RecordBatch with one row
|
|
104
|
+
table = pa.table(batch) # pyarrow.Table, through the PyCapsule interface
|
|
105
|
+
|
|
106
|
+
# Many batches with one schema: one row each.
|
|
107
|
+
rows = nda.encode_batches([batch, other_batch])
|
|
108
|
+
batches = nda.NdRecordBatch.from_arrow(rows) # a list, one NdRecordBatch per row
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`NdRecordBatch.from_arrow` accepts a `RecordBatch`, a `Table` or a
|
|
112
|
+
`RecordBatchReader`. It returns one `NdRecordBatch` for each row.
|
|
113
|
+
The global attributes travel in the `__global_attrs__` column.
|
|
114
|
+
|
|
115
|
+
### Files
|
|
116
|
+
|
|
117
|
+
Parquet and Arrow IPC keep the field metadata, so the nd columns survive a
|
|
118
|
+
write and a read:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
import pyarrow.parquet as pq
|
|
122
|
+
|
|
123
|
+
pq.write_table(pa.Table.from_batches([rows]), "data.parquet")
|
|
124
|
+
batches = nda.NdRecordBatch.from_arrow(pq.read_table("data.parquet"))
|
|
125
|
+
|
|
126
|
+
with pa.OSFile("data.arrow", "wb") as sink, pa.ipc.new_file(sink, rows.schema) as writer:
|
|
127
|
+
writer.write_batch(rows)
|
|
128
|
+
with pa.memory_map("data.arrow") as source:
|
|
129
|
+
batches = nda.NdRecordBatch.from_arrow(pa.ipc.open_file(source).read_all())
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Other libraries
|
|
133
|
+
|
|
134
|
+
`NdArray`, `NdColumn` and `NdRecordBatch` implement the Arrow PyCapsule
|
|
135
|
+
interface (`__arrow_c_array__`, `__arrow_c_stream__`, `__arrow_c_schema__`).
|
|
136
|
+
pyarrow, polars, DuckDB and other Arrow libraries read them without a copy,
|
|
137
|
+
for example `pa.array(col)` or `pa.table(batch)`.
|
|
138
|
+
|
|
139
|
+
## xarray
|
|
140
|
+
|
|
141
|
+
`pip install nd-arrow-array[xarray]` adds xarray and cftime.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
import xarray as xr
|
|
145
|
+
|
|
146
|
+
ds = xr.Dataset(
|
|
147
|
+
{"air": (("time", "lat"), np.zeros((2, 3)), {"units": "K"})},
|
|
148
|
+
coords={"time": np.array(["2020-01-01", "2020-01-02"], "datetime64[ns]"),
|
|
149
|
+
"lat": [-10.0, 0.0, 10.0]},
|
|
150
|
+
)
|
|
151
|
+
batch = nda.NdRecordBatch.from_xarray(ds) # all variables and coordinates
|
|
152
|
+
batch.to_xarray() # an xarray.Dataset
|
|
153
|
+
|
|
154
|
+
a = nda.NdArray.from_xarray(ds["air"]) # values, dims and attrs only
|
|
155
|
+
a.to_xarray(name="air") # an xarray.DataArray
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
- `NdRecordBatch.from_xarray` accepts a `Dataset` or a `DataArray`.
|
|
159
|
+
Each variable and each coordinate becomes a column.
|
|
160
|
+
- A 1-D column with the name of its dimension becomes an index coordinate.
|
|
161
|
+
- Other coordinates go into the CF `coordinates` attribute of the data
|
|
162
|
+
variables. A coordinate that no data variable uses goes into the
|
|
163
|
+
`coordinates` attribute of the batch.
|
|
164
|
+
- `NdArray.from_xarray` does not keep coordinates.
|
|
165
|
+
- In `to_xarray`, nulls become `NaN` or `NaT`. Integer and bool arrays with
|
|
166
|
+
nulls become `float64`, as in xarray CF decoding.
|
|
167
|
+
- Object arrays must hold `str` or cftime dates. `None` and `NaN` become nulls.
|
|
168
|
+
- cftime dates become `timestamp[us]` in the standard calendar:
|
|
169
|
+
- `standard`, `gregorian`, `proleptic_gregorian` and `julian` keep the
|
|
170
|
+
exact instant;
|
|
171
|
+
- `noleap`, `365_day`, `all_leap`, `366_day` and `360_day` keep the year,
|
|
172
|
+
month, day and time. A date that the standard calendar does not have,
|
|
173
|
+
such as 30 February, raises `ValueError`. Use
|
|
174
|
+
`convert_calendar(..., align_on="date")` in xarray first.
|
|
175
|
+
|
|
176
|
+
## Rules
|
|
177
|
+
|
|
178
|
+
- NumPy dtypes: `bool`, signed and unsigned integers, `float16`, `float32`,
|
|
179
|
+
`float64`, `datetime64` and `timedelta64` (`s`, `ms`, `us`, `ns`) and `str`.
|
|
180
|
+
Other dtypes raise `TypeError`.
|
|
181
|
+
- A `numpy.ma.MaskedArray` mask and `NaT` become nulls. `NaN` stays a value.
|
|
182
|
+
- `to_numpy()` returns a `MaskedArray` when the values have nulls.
|
|
183
|
+
- `attrs` returns a copy. Assign a new dict, or use `set_attr` and `del_attr`.
|
|
184
|
+
- All classes implement the Arrow PyCapsule interface, so pyarrow, polars
|
|
185
|
+
and DuckDB can read them.
|
|
186
|
+
- The package ships type stubs (`py.typed`), so mypy and pyright check calls.
|
|
187
|
+
|
|
188
|
+
## Test
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
pip install pytest mypy numpy pyarrow xarray cftime
|
|
192
|
+
maturin develop && pytest
|
|
193
|
+
python -m mypy.stubtest nd_arrow_array # stubs match the module
|
|
194
|
+
mypy --strict --warn-unused-ignores tests/typing
|
|
195
|
+
```
|
|
196
|
+
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# nd-arrow-array (Python)
|
|
2
|
+
|
|
3
|
+
Python bindings for the `nd_arrow.array` Arrow extension type.
|
|
4
|
+
The PyPI package is `nd-arrow-array`. The import name is `nd_arrow_array`.
|
|
5
|
+
The bindings move N-dimensional arrays between NumPy, pyarrow and the
|
|
6
|
+
format in [FORMAT.md](https://github.com/robinskil/nd-arrow-array/blob/main/FORMAT.md).
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install nd-arrow-array # NumPy only
|
|
12
|
+
pip install nd-arrow-array[arrow] # with pyarrow
|
|
13
|
+
pip install nd-arrow-array[xarray] # with xarray and cftime
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
To build from source, run `pip install maturin` and `maturin develop` in this directory.
|
|
17
|
+
|
|
18
|
+
## Classes
|
|
19
|
+
|
|
20
|
+
| Class | Content |
|
|
21
|
+
|---|---|
|
|
22
|
+
| `NdArray` | One array: values, named dimensions, attributes. |
|
|
23
|
+
| `NdColumn` | An Arrow column. Each row is one `NdArray` or `None`. |
|
|
24
|
+
| `NdRecordBatch` | Named arrays on one shared grid, with global attributes. |
|
|
25
|
+
|
|
26
|
+
## Example
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import numpy as np
|
|
30
|
+
import nd_arrow_array as nda
|
|
31
|
+
|
|
32
|
+
a = nda.NdArray.from_numpy(
|
|
33
|
+
np.arange(6.0).reshape(2, 3),
|
|
34
|
+
dims=["time", "lat"],
|
|
35
|
+
attrs={"units": "K", "valid_range": np.array([0, 400], dtype="float32")},
|
|
36
|
+
)
|
|
37
|
+
a.sizes # {'time': 2, 'lat': 3}
|
|
38
|
+
a.set_attr("long_name", "sea surface temperature")
|
|
39
|
+
a.to_numpy()
|
|
40
|
+
|
|
41
|
+
col = nda.NdColumn.from_arrays([a, None])
|
|
42
|
+
arr = col.to_arrow() # pyarrow array
|
|
43
|
+
nda.NdColumn.from_arrow(arr)[0] == a # True
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## pyarrow
|
|
47
|
+
|
|
48
|
+
`pip install nd-arrow-array[arrow]` adds pyarrow. pyarrow sees an nd column as a
|
|
49
|
+
`struct` column. The field metadata `ARROW:extension:name = nd_arrow.array`
|
|
50
|
+
marks it as an nd column. See [FORMAT.md](https://github.com/robinskil/nd-arrow-array/blob/main/FORMAT.md).
|
|
51
|
+
|
|
52
|
+
### Arrays and columns
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import pyarrow as pa
|
|
56
|
+
|
|
57
|
+
# An array from flat pyarrow values in C order.
|
|
58
|
+
a = nda.NdArray(pa.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]),
|
|
59
|
+
dims={"time": 2, "lat": 3}, attrs={"units": "K"})
|
|
60
|
+
a.values # the flat values as a pyarrow.Array
|
|
61
|
+
|
|
62
|
+
col = nda.NdColumn.from_arrays([a, None])
|
|
63
|
+
arr = col.to_arrow() # pyarrow.StructArray, one row per array
|
|
64
|
+
nda.NdColumn.from_arrow(arr) # back to an NdColumn
|
|
65
|
+
pa.field(col).metadata # the extension name and version
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Record batches and tables
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
lat = nda.NdArray.from_numpy(np.array([-10.0, 0.0, 10.0]), dims=["lat"])
|
|
72
|
+
batch = nda.NdRecordBatch({"sst": a, "lat": lat}, attrs={"title": "demo"})
|
|
73
|
+
|
|
74
|
+
rb = batch.to_arrow() # pyarrow.RecordBatch with one row
|
|
75
|
+
table = pa.table(batch) # pyarrow.Table, through the PyCapsule interface
|
|
76
|
+
|
|
77
|
+
# Many batches with one schema: one row each.
|
|
78
|
+
rows = nda.encode_batches([batch, other_batch])
|
|
79
|
+
batches = nda.NdRecordBatch.from_arrow(rows) # a list, one NdRecordBatch per row
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`NdRecordBatch.from_arrow` accepts a `RecordBatch`, a `Table` or a
|
|
83
|
+
`RecordBatchReader`. It returns one `NdRecordBatch` for each row.
|
|
84
|
+
The global attributes travel in the `__global_attrs__` column.
|
|
85
|
+
|
|
86
|
+
### Files
|
|
87
|
+
|
|
88
|
+
Parquet and Arrow IPC keep the field metadata, so the nd columns survive a
|
|
89
|
+
write and a read:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import pyarrow.parquet as pq
|
|
93
|
+
|
|
94
|
+
pq.write_table(pa.Table.from_batches([rows]), "data.parquet")
|
|
95
|
+
batches = nda.NdRecordBatch.from_arrow(pq.read_table("data.parquet"))
|
|
96
|
+
|
|
97
|
+
with pa.OSFile("data.arrow", "wb") as sink, pa.ipc.new_file(sink, rows.schema) as writer:
|
|
98
|
+
writer.write_batch(rows)
|
|
99
|
+
with pa.memory_map("data.arrow") as source:
|
|
100
|
+
batches = nda.NdRecordBatch.from_arrow(pa.ipc.open_file(source).read_all())
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Other libraries
|
|
104
|
+
|
|
105
|
+
`NdArray`, `NdColumn` and `NdRecordBatch` implement the Arrow PyCapsule
|
|
106
|
+
interface (`__arrow_c_array__`, `__arrow_c_stream__`, `__arrow_c_schema__`).
|
|
107
|
+
pyarrow, polars, DuckDB and other Arrow libraries read them without a copy,
|
|
108
|
+
for example `pa.array(col)` or `pa.table(batch)`.
|
|
109
|
+
|
|
110
|
+
## xarray
|
|
111
|
+
|
|
112
|
+
`pip install nd-arrow-array[xarray]` adds xarray and cftime.
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
import xarray as xr
|
|
116
|
+
|
|
117
|
+
ds = xr.Dataset(
|
|
118
|
+
{"air": (("time", "lat"), np.zeros((2, 3)), {"units": "K"})},
|
|
119
|
+
coords={"time": np.array(["2020-01-01", "2020-01-02"], "datetime64[ns]"),
|
|
120
|
+
"lat": [-10.0, 0.0, 10.0]},
|
|
121
|
+
)
|
|
122
|
+
batch = nda.NdRecordBatch.from_xarray(ds) # all variables and coordinates
|
|
123
|
+
batch.to_xarray() # an xarray.Dataset
|
|
124
|
+
|
|
125
|
+
a = nda.NdArray.from_xarray(ds["air"]) # values, dims and attrs only
|
|
126
|
+
a.to_xarray(name="air") # an xarray.DataArray
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
- `NdRecordBatch.from_xarray` accepts a `Dataset` or a `DataArray`.
|
|
130
|
+
Each variable and each coordinate becomes a column.
|
|
131
|
+
- A 1-D column with the name of its dimension becomes an index coordinate.
|
|
132
|
+
- Other coordinates go into the CF `coordinates` attribute of the data
|
|
133
|
+
variables. A coordinate that no data variable uses goes into the
|
|
134
|
+
`coordinates` attribute of the batch.
|
|
135
|
+
- `NdArray.from_xarray` does not keep coordinates.
|
|
136
|
+
- In `to_xarray`, nulls become `NaN` or `NaT`. Integer and bool arrays with
|
|
137
|
+
nulls become `float64`, as in xarray CF decoding.
|
|
138
|
+
- Object arrays must hold `str` or cftime dates. `None` and `NaN` become nulls.
|
|
139
|
+
- cftime dates become `timestamp[us]` in the standard calendar:
|
|
140
|
+
- `standard`, `gregorian`, `proleptic_gregorian` and `julian` keep the
|
|
141
|
+
exact instant;
|
|
142
|
+
- `noleap`, `365_day`, `all_leap`, `366_day` and `360_day` keep the year,
|
|
143
|
+
month, day and time. A date that the standard calendar does not have,
|
|
144
|
+
such as 30 February, raises `ValueError`. Use
|
|
145
|
+
`convert_calendar(..., align_on="date")` in xarray first.
|
|
146
|
+
|
|
147
|
+
## Rules
|
|
148
|
+
|
|
149
|
+
- NumPy dtypes: `bool`, signed and unsigned integers, `float16`, `float32`,
|
|
150
|
+
`float64`, `datetime64` and `timedelta64` (`s`, `ms`, `us`, `ns`) and `str`.
|
|
151
|
+
Other dtypes raise `TypeError`.
|
|
152
|
+
- A `numpy.ma.MaskedArray` mask and `NaT` become nulls. `NaN` stays a value.
|
|
153
|
+
- `to_numpy()` returns a `MaskedArray` when the values have nulls.
|
|
154
|
+
- `attrs` returns a copy. Assign a new dict, or use `set_attr` and `del_attr`.
|
|
155
|
+
- All classes implement the Arrow PyCapsule interface, so pyarrow, polars
|
|
156
|
+
and DuckDB can read them.
|
|
157
|
+
- The package ships type stubs (`py.typed`), so mypy and pyright check calls.
|
|
158
|
+
|
|
159
|
+
## Test
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
pip install pytest mypy numpy pyarrow xarray cftime
|
|
163
|
+
maturin develop && pytest
|
|
164
|
+
python -m mypy.stubtest nd_arrow_array # stubs match the module
|
|
165
|
+
mypy --strict --warn-unused-ignores tests/typing
|
|
166
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "nd-arrow-array"
|
|
3
|
+
version = "0.5.0"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
rust-version = "1.85"
|
|
6
|
+
description = "N-dimensional Arrow arrays as an Arrow extension type"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
repository = "https://github.com/robinskil/nd-arrow-array"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
keywords = ["arrow", "ndarray", "tensor", "xarray", "netcdf"]
|
|
11
|
+
categories = ["data-structures", "science", "encoding"]
|
|
12
|
+
|
|
13
|
+
[package.metadata.docs.rs]
|
|
14
|
+
all-features = true
|
|
15
|
+
|
|
16
|
+
[dependencies]
|
|
17
|
+
arrow = { version = "58.4", default-features = false }
|
|
18
|
+
arrow-schema = "58.4"
|
|
19
|
+
serde = { version = "1", features = ["derive"] }
|
|
20
|
+
serde_json = "1"
|
|
21
|
+
thiserror = "2"
|
|
22
|
+
rayon = { version = "1.10", optional = true }
|
|
23
|
+
ndarray = { version = "0.17", optional = true }
|
|
24
|
+
|
|
25
|
+
[features]
|
|
26
|
+
parallel = ["dep:rayon"]
|
|
27
|
+
ndarray = ["dep:ndarray"]
|
|
28
|
+
|
|
29
|
+
[dev-dependencies]
|
|
30
|
+
arrow = { version = "58.4", default-features = false, features = ["ipc"] }
|
|
31
|
+
criterion = "0.5"
|
|
32
|
+
proptest = "1"
|
|
33
|
+
|
|
34
|
+
[[bench]]
|
|
35
|
+
name = "kernels"
|
|
36
|
+
harness = false
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|