Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support numpy 1.24 ragged array conversion #107

Merged
merged 1 commit into from
Jan 9, 2023
Merged
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
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