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: pd.concat doesn't recognise GeoDataFrame as expected output

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

import pandas as pd
import geopandas as gpd


def df() -> pd.DataFrame:
    return pd.DataFrame(
        {
            "City": ["Buenos Aires", "Brasilia", "Santiago", "Bogota", "Caracas"],
            "Country": ["Argentina", "Brazil", "Chile", "Colombia", "Venezuela"],
            "Latitude": [-34.58, -15.78, -33.45, 4.60, 10.48],
            "Longitude": [-58.66, -47.91, -70.66, -74.08, -66.86],
        }
    )


def gdf_points_from_xy(df: pd.DataFrame) -> gpd.GeoDataFrame:
    return gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))


def concat_gdf(gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
    # pycharm linter complains:
    # Expected type 'GeoDataFrame', got 'Union[DataFrame, Series]' instead 
    return pd.concat([gdf, gdf])


def test_pd_concat():
    _df = df()
    _gdf = gdf_points_from_xy(_df)
    catted = concat_gdf(_gdf)

    assert isinstance(catted, gpd.GeoDataFrame) #  No problem

Problem description

Pycharm complains about the return type of pd.concat when the method calling it expects the return type to be gpd.GeoDataFrame. See screenshot.

image

One workaround is to explicitly create a new GeoDataFrame object after concat as discussed in this thread.

Is there something obvious we can do with the GeoPandas library to overcome this?

Package versions

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.9.0
pandas     : 1.3.2
fiona      : 1.8.20
numpy      : 1.21.1
shapely    : 1.7.1
rtree      : 0.9.7
pyproj     : 3.1.0
matplotlib : 3.4.3
mapclassify: None
geopy      : 2.2.0
psycopg2   : 2.9.1 (dt dec pq3 ext lo64)
geoalchemy2: 0.8.5
pyarrow    : 5.0.0
pygeos     : None

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
martinfleiscommented, Aug 26, 2021

There’s a bit more on this in mypy docs (https://mypy.readthedocs.io/en/latest/kinds_of_types.html#the-type-of-class-objects, and https://mypy.readthedocs.io/en/latest/class_basics.html). I want to go through that in the evening to understand it a bit before reporting to pandas.

0reactions
jorisvandenbosschecommented, Aug 26, 2021

That SO question/answer might be about (sub)class objects and not (sub)class instances?

Read more comments on GitHub >

github_iconTop Results From Across the Web

BUG: pd.concat([..], axis=1) fails · Issue #1230 - GitHub
Raise an exception when concat is called with arguments where the geometry column is not unique · Mangle the repeated columns with a...
Read more >
Pandas concat: why is the `DataFrame` with duplicated index ...
However, as the error says, it thinks the shape based off the indices is (5, 2) because of this address sharing two different...
Read more >
Managing Projections — GeoPandas 0.12.2+0.gefcb367.dirty ...
Projection for multiple geometry columns#. GeoPandas 0.8 implements support for different projections assigned to different geometry columns of the same ...
Read more >
Joining (concat) list of similar dataframes in geopandas?
Great answer, one improvement: rdf = gpd.GeoDataFrame(pd.concat(dataframesList, ignore_index=True), crs=dataframesList[0].crs) . Now new ...
Read more >
combine duplicate rows pandas
When you concat() two pandas DataFrames on rows, it creates a new ... you will always return multiple results, hence the MERGE statement...
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