question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: iloc indexing Dataframe with only one column (`geometry`) raises TypeError

See original GitHub issue
  • 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

from shapely.geometry import Polygon
from geopandas import GeoSeries, GeoDataFrame

polys = GeoSeries(
    [
        Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
        Polygon([(1, 1), (3, 1), (3, 3), (1, 3)]),
    ]
)

gdf = GeoDataFrame({"geometry": polys, "number": range(len(polys))})
gdf.iloc[[0]] = gdf.iloc[[1]]  # works

gdf = GeoDataFrame({"geometry": polys})
gdf.iloc[[0]] = gdf.iloc[[1]]  # does not work

Problem description

One may not always have other columns than geometry when wanting to use .iloc

Example of code where I first encountered this issue:

from shapely.geometry import Polygon
from geopandas import GeoSeries, GeoDataFrame, overlay

polys = GeoSeries(
    [
        Polygon([(0, 0), (2, 0), (2, 2), (0, 2)]),
        Polygon([(1, 1), (3, 1), (3, 3), (1, 3)]),
        Polygon([(2, 2), (4, 2), (4, 4), (2, 4)]),
    ]
)

gdf = GeoDataFrame({"geometry": polys})

for i in range(0, len(gdf) - 1):
    gdf.iloc[[i]] = overlay(gdf.iloc[[i]], gdf.iloc[[i + 1]], how="difference")

Expected Output

No TypeError.

Output of geopandas.show_versions()

Failed CDLL(libgeos_c.so.1) Failed CDLL(libgeos_c.so)

SYSTEM INFO

python : 3.9.5 (default, May 11 2021, 08:20:37) [GCC 10.3.0] executable : /home/username/Documents/project_name/env/bin/python machine : Linux-5.11.0-18-generic-x86_64-with-glibc2.33

GEOS, GDAL, PROJ INFO

GEOS : None GEOS lib : None GDAL : 3.3.0 GDAL data dir: /home/username/Documents/project_name/env/lib/python3.9/site-packages/fiona/gdal_data PROJ : 8.0.1 PROJ data dir: /home/username/Documents/project_name/env/lib/python3.9/site-packages/pyproj/proj_dir/share/proj

PYTHON DEPENDENCIES

geopandas : 0.9.0 pandas : 1.2.4 fiona : 1.8.20 numpy : 1.20.3 shapely : 1.7.1 rtree : 0.9.7 pyproj : 3.1.0 matplotlib : 3.4.2 mapclassify: None geopy : 2.1.0 psycopg2 : None geoalchemy2: None pyarrow : None pygeos : None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
martinfleiscommented, Jun 15, 2021

Thanks for the report! I can confirm that this is a bug in the way how GeometryArray handles gdf (it does not 😃).

I think we need to add one more condition handling this situation here: https://github.com/geopandas/geopandas/blob/cb88dd4a9100bb990f6eb9b286e1ef3a0b185e3c/geopandas/array.py#L371

Would you like to try to fix it?

1reaction
Akylzhancommented, Jun 19, 2021

@JoakimJoensuu, do you plan to work on this issue? If no, I want to try to fix

Read more comments on GitHub >

github_iconTop Results From Across the Web

REGR: indexing doesn't return geometry but Series if ... - GitHub
If there's a single column in a GeoDataFrame, indexing using df.loc[0].geometry returns a Series instead of a shapely geometry (as it did ...
Read more >
Pandas drop_duplicates method not working on dataframe ...
drop_duplicates won't work with lists in your dataframe as the error message implies. However, you can drop duplicates on the dataframe casted as...
Read more >
KeyError Pandas – How To Fix - Data Independent
Pandas KeyError - This annoying error means that Pandas can not find your column name in your dataframe. Here's how to fix this...
Read more >
Indexing and selecting data — pandas 1.5.2 documentation
iloc will raise IndexError if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing. (this conforms with Python/NumPy ...
Read more >
Changelog — GeoPandas 0.12.2+0.gefcb367.dirty ...
Fix GeoDataFrame.iloc raising a TypeError when indexing a GeoDataFrame with only a single column of GeometryDtype (#1970).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found