pyproj icon indicating copy to clipboard operation
pyproj copied to clipboard

Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future

Open mwtoews opened this issue 10 months ago • 1 comments

The CI logs for pyproj show a few common warning messages, e.g. here

test/test_geod.py: 72 warnings
  /home/runner/work/pyproj/pyproj/pyproj/geod.py:398: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
    return self._inv_point(

test/test_geod.py: 20 warnings
  /home/runner/work/pyproj/pyproj/pyproj/geod.py:306: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
    return self._fwd_point(
...
test/test_transformer.py: 40 warnings
  /home/runner/work/pyproj/pyproj/pyproj/transformer.py:817: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
    return self._transformer._transform_point(

Code Sample

A small replicator for _transform_point is:

import warnings

import numpy as np
from pyproj import Transformer

warnings.simplefilter("error")

transformer = Transformer.from_crs(4326, 32618, always_xy=True)
transformer.transform(np.array([1]), np.array([2]))

with error

Traceback (most recent call last): File "", line 1, in File "/tmp/z/lib/python3.10/site-packages/pyproj/transformer.py", line 817, in transform return self._transformer._transform_point( File "pyproj/_transformer.pyx", line 743, in pyproj._transformer._Transformer._transform_point DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

This is because of a try/except block that attempts a scalar transform in pyproj/transformer.py#L815-L827. The attempt with non-scalar np.array([1]) throws a warning/error when assigned to scalar double coord_x in pyproj/_transformer.pyx#L743.

Array inputs should use _transform instead of _transform_point, which is intended for scalars.

I haven't checked the other instances with _inv_point or _fwd_point, but I'd expect similar.

Environment Information

  • Output from: pyproj -v
pyproj info:
    pyproj: 3.7.0
PROJ (runtime): 9.4.1
PROJ (compiled): 9.4.1
  data dir: /tmp/z/lib/python3.10/site-packages/pyproj/proj_dir/share/proj
user_data_dir: /home/mtoews/.local/share/proj
PROJ DATA (recommended version): 1.18
PROJ Database: 1.3
EPSG Database: v11.006 [2024-03-13]
ESRI Database: ArcGIS Pro 3.3 [2024-05-07]
IGNF Database: 3.1.0 [2019-05-24]

System:
    python: 3.10.12 (main, Nov  6 2024, 20:22:13) [GCC 11.4.0]
executable: /tmp/z/bin/python3
   machine: Linux-6.8.0-51-generic-x86_64-with-glibc2.35

Python deps:
   certifi: 2024.12.14
    Cython: None
setuptools: 59.6.0
       pip: 22.0.2

mwtoews avatar Jan 14 '25 03:01 mwtoews

Duplicate of #1307

snowman2 avatar Jan 14 '25 17:01 snowman2