Skip to content

BUG: Geopandas with pygeos removes z-coordinate if the first z-coordinate is NaN #1888

@woest

Description

@woest
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of geopandas.

  • (optional) I have confirmed this bug exists on the master branch of geopandas.


Code Sample, a copy-pastable example

import geopandas as gpd
import pandas as pd
import numpy as np
import shapely.geometry

coords1 = np.array([
    [1, 2, 3],
    [1, 1, np.NaN],
])

coords2 = np.array([
    [1, 1, np.NaN],
    [1, 2, 3],
])

for coords in [coords1, coords2]:
    geom = shapely.geometry.LineString(coords)
    print(np.array(geom.coords))
    df = gpd.GeoDataFrame(pd.DataFrame({'geometry': [geom]}))
    print(np.array(df.geometry.iat[0].coords))
    print('\n')

Problem description

In Geopandas 0.9.0, if the first z-value of a linestring is NaN, the z-dimension is removed. This seems to be a new bug, as geopandas <0.9 did not have this problem.

Expected Output (on 0.8)

[[ 1.  2.  3.]
 [ 1.  1. nan]]
[[ 1.  2.  3.]
 [ 1.  1. nan]]


[[ 1.  1. nan]
 [ 1.  2.  3.]]
[[ 1.  1. nan]
 [ 1.  2.  3.]]

Output on 0.9.0

[[ 1.  2.  3.]
 [ 1.  1. nan]]
[[ 1.  2.  3.]
 [ 1.  1. nan]]


[[ 1.  1. nan]
 [ 1.  2.  3.]]
[[1. 1.]
 [1. 2.]]

Output of geopandas.show_versions()

Details

python : 3.8.8 | packaged by conda-forge | (default, Feb 20 2021, 15:50:08) [MSC v.1916 64 bit (AMD64)]
executable : C:\Users\USERNAME\Miniconda3\python.exe
machine : Windows-10-10.0.18362-SP0

GEOS, GDAL, PROJ INFO

GEOS : None
GEOS lib : None
GDAL : 3.2.1
GDAL data dir: None
PROJ : 7.2.0
PROJ data dir: C:\Users\USERNAME\Miniconda3\Library\share\proj

PYTHON DEPENDENCIES

geopandas : 0.9.0
pandas : 1.2.3
fiona : 1.8.18
numpy : 1.20.1
shapely : 1.7.1
rtree : 0.9.7
pyproj : 3.0.1
matplotlib : 3.3.4
mapclassify: None
geopy : 2.1.0
psycopg2 : None
geoalchemy2: None
pyarrow : None
pygeos : 0.9

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions