Skip to content

Commit a858a28

Browse files
authored
Support numpy 1.24 ragged array conversion (#107)
1 parent 8fb0a5d commit a858a28

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

spatialpandas/geometry/basefixed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from ..geometry.base import Geometry, GeometryArray, GeometryDtype
77
from ..geometry.baselist import _lexographic_lt
8+
from ..utils import _asarray_maybe_ragged
89
from ._algorithms.bounds import (bounds_interleaved, total_bounds_interleaved,
910
total_bounds_interleaved_1d)
1011

@@ -108,7 +109,7 @@ def invalid_array():
108109
else:
109110
invalid_array()
110111
else:
111-
array = np.asarray(array)
112+
array = _asarray_maybe_ragged(array)
112113
if array.dtype.kind == 'O':
113114
if array.ndim != 1:
114115
invalid_array()

spatialpandas/io/parquet.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ def _perform_read_parquet_dask(
405405
filesystem=filesystem,
406406
engine='pyarrow',
407407
categories=categories,
408-
gather_statistics=False,
409408
storage_options=storage_options,
410409
**engine_kwargs,
411410
)._meta

spatialpandas/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import numpy as np
24
from numba import jit
35

@@ -28,3 +30,34 @@ def _data2coord(vals, val_range, n):
2830
res[res < 0] = 0
2931
res[res > n - 1] = n - 1
3032
return res
33+
34+
35+
def _asarray_maybe_ragged(input):
36+
"""Convert input into a single numpy array, even if it is a ragged array.
37+
38+
Prior to numpy 1.24 just np.asarray(input) suffices as it emits a
39+
np.VisibleDeprecationWarning if the input is ragged, i.e. can't be
40+
converted to a single numpy array, but still completes the conversion by
41+
creating a numpy array of multiple subarrays. From 1.24 onwards attempting
42+
to do this raises a ValueError instead. This function therefore tries the
43+
simple conversion and if this fails uses the ragged-supporting conversion
44+
of np.asarray(input, type=object).
45+
46+
To demonstrate that this works with numpy < 1.24 it converts
47+
VisibleDeprecationWarnings into errors so that they are handled the same
48+
as for numpy >= 1.24.
49+
50+
Args:
51+
input: ArrayLike | list[ArrayLike | None]
52+
53+
Returns:
54+
NumPy array.
55+
"""
56+
with warnings.catch_warnings():
57+
warnings.simplefilter('error', np.VisibleDeprecationWarning)
58+
try:
59+
array = np.asarray(input)
60+
except (ValueError, np.VisibleDeprecationWarning):
61+
array = np.asarray(input, dtype=object)
62+
63+
return array

0 commit comments

Comments
 (0)