Skip to content

Commit

Permalink
Support numpy 1.24 ragged array conversion (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 authored Jan 9, 2023
1 parent 8fb0a5d commit a858a28
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 2 additions & 1 deletion spatialpandas/geometry/basefixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ..geometry.base import Geometry, GeometryArray, GeometryDtype
from ..geometry.baselist import _lexographic_lt
from ..utils import _asarray_maybe_ragged
from ._algorithms.bounds import (bounds_interleaved, total_bounds_interleaved,
total_bounds_interleaved_1d)

Expand Down Expand Up @@ -108,7 +109,7 @@ def invalid_array():
else:
invalid_array()
else:
array = np.asarray(array)
array = _asarray_maybe_ragged(array)
if array.dtype.kind == 'O':
if array.ndim != 1:
invalid_array()
Expand Down
1 change: 0 additions & 1 deletion spatialpandas/io/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ def _perform_read_parquet_dask(
filesystem=filesystem,
engine='pyarrow',
categories=categories,
gather_statistics=False,
storage_options=storage_options,
**engine_kwargs,
)._meta
Expand Down
33 changes: 33 additions & 0 deletions spatialpandas/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from numba import jit

Expand Down Expand Up @@ -28,3 +30,34 @@ def _data2coord(vals, val_range, n):
res[res < 0] = 0
res[res > n - 1] = n - 1
return res


def _asarray_maybe_ragged(input):
"""Convert input into a single numpy array, even if it is a ragged array.
Prior to numpy 1.24 just np.asarray(input) suffices as it emits a
np.VisibleDeprecationWarning if the input is ragged, i.e. can't be
converted to a single numpy array, but still completes the conversion by
creating a numpy array of multiple subarrays. From 1.24 onwards attempting
to do this raises a ValueError instead. This function therefore tries the
simple conversion and if this fails uses the ragged-supporting conversion
of np.asarray(input, type=object).
To demonstrate that this works with numpy < 1.24 it converts
VisibleDeprecationWarnings into errors so that they are handled the same
as for numpy >= 1.24.
Args:
input: ArrayLike | list[ArrayLike | None]
Returns:
NumPy array.
"""
with warnings.catch_warnings():
warnings.simplefilter('error', np.VisibleDeprecationWarning)
try:
array = np.asarray(input)
except (ValueError, np.VisibleDeprecationWarning):
array = np.asarray(input, dtype=object)

return array

0 comments on commit a858a28

Please sign in to comment.