Naveed
Naveed

Reputation: 602

Merging geopandas dataframe and converting to json throws "Maximum recursion level reached" error

Dataframe df has following columns:

['country_code', 'confirmed_cases', 'count_date']

gdf (geopandas dataframe) has following columns:

['country_code', 'geometry']

All of the following work:

df.to_json()
gdf.to_json()
gdf.geometry.to_json()

But when I merge both I cant convert to json.

merged_df = df.merge(gdf, how='left')

merged_df.country_code.to_json() #works
merged_df.count_date.to_json() #works
merged_df.confirmed_cases.to_json() #works

merged_df.to_json() #THROWS MAX RECURSION ERROR
merged_df.geometry.to_json() #THROWS MAX RECURSION ERROR
merged_df.iloc[:1].to_json() #THROWS MAX RECURSION ERROR

Why is the same 'geometry' column behaving differently in two dataframes?

Upvotes: 1

Views: 925

Answers (1)

martinfleis
martinfleis

Reputation: 7814

Merge the files in the other way. Since gdf is GeoDataFrame, to_json has to deal with geometry column. Pandas can't to that by itself. If you merge gdf to df as you did, your result is pandas.DataFrame. If you merge the other way, your result will be geopandas.GeoDataFrame which can handle exporting geometries to json.

merged_df = gdf.merge(df, how='right')

Upvotes: 2

Related Questions