From 7e4c0d8a87468b24a1351d96ebf08f1a02478da4 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Fri, 6 Jan 2023 11:14:35 +0000 Subject: [PATCH] Support numpy 1.24 ragged array conversion --- spatialpandas/geometry/basefixed.py | 3 ++- spatialpandas/io/parquet.py | 1 - spatialpandas/utils.py | 33 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/spatialpandas/geometry/basefixed.py b/spatialpandas/geometry/basefixed.py index a959a00..35bb5ba 100644 --- a/spatialpandas/geometry/basefixed.py +++ b/spatialpandas/geometry/basefixed.py @@ -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) @@ -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() diff --git a/spatialpandas/io/parquet.py b/spatialpandas/io/parquet.py index ddfff19..084f395 100644 --- a/spatialpandas/io/parquet.py +++ b/spatialpandas/io/parquet.py @@ -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 diff --git a/spatialpandas/utils.py b/spatialpandas/utils.py index dae5efe..cc9d9c2 100644 --- a/spatialpandas/utils.py +++ b/spatialpandas/utils.py @@ -1,3 +1,5 @@ +import warnings + import numpy as np from numba import jit @@ -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