1
votes

I have created a geodataframe containing 3 columns: Country Code, Infection Rate and Geometry. The geodataframe is generated by importing a shapefile from http://www.naturalearthdata.com/downloads/10m-cultural-vectors/ into a geodataframe and merging it with an 'infections' dataframe

Infections.head(5) returns the following:

Country_Code    Infection_Rate
0   CHN 0.00088
1   VNM 0.00661
2   BRA 0.00240
3   KOR 0.00732
4   RUS 0.00156

The geodataframe is imported as follows:

## importing shapefile
shapefile = os.path.expanduser('ne_10m_admin_0_countries_lakes.shp')
geo = gpd.read_file(shapefile)[['ADM0_A3', 'geometry']].to_crs('+proj=robin') 

geo.head(5) returns the following:

ADM0_A3 geometry
0   IDN MULTIPOLYGON (((11108970.260 445285.130, 11108...
1   MYS MULTIPOLYGON (((11108970.260 445285.130, 11108...
2   CHL MULTIPOLYGON (((-6477059.899 -1872360.838, -64...
3   BOL POLYGON ((-6477059.899 -1872360.838, -6477072....
4   PER MULTIPOLYGON (((-6477059.899 -1872360.838, -64...

Then, i merge the 'geo' dataframe with the 'infections' dataframe as follows:

geoinfect = inf.merge(geo, left_on = 'Country_Code', right_on = 'ADM0_A3')

geoinfect.head(5) returns the following:

Country_Code    Infection_Rate  geometry
0   CHN 0.00088 MULTIPOLYGON (((7341533.511 3229629.092, 73418...
1   VNM 0.00661 MULTIPOLYGON (((10060615.963 1572682.727, 1006...
2   BRA 0.00240 MULTIPOLYGON (((-5222198.318 -3228130.673, -52...
3   KOR 0.00732 MULTIPOLYGON (((11252435.443 4130015.051, 1125...
4   RUS 0.00156 MULTIPOLYGON (((7242168.542 5240460.51

When i check the dtypes of the resulting geoinfect dataframe i get the following output:

Country_Code        object
Infection_Rate     float64
geometry          geometry
dtype: object

I plot the data using the following lines

## Some plot settings
colors = 9
cmap = 'Blues'
figsize = (16, 10)
plotvar = 'Infection_Rate'
scheme = 'equal interval'

## First plot
ax = geoinfect.plot(plotvar, cmap=cmap, figsize=figsize, k = colors, scheme = scheme,  legend=True)

This returns me the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-165-5d36d985a633> in <module>
      8 
      9 ## First plot
---> 10 geoinfect.plot(plotvar, cmap=cmap, figsize=figsize, k = colors, scheme = scheme,  legend=True)

/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   2940                           fontsize=fontsize, colormap=colormap, table=table,
   2941                           yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 2942                           sort_columns=sort_columns, **kwds)
   2943     __call__.__doc__ = plot_frame.__doc__
   2944 

/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
   1971                  yerr=yerr, xerr=xerr,
   1972                  secondary_y=secondary_y, sort_columns=sort_columns,
-> 1973                  **kwds)
   1974 
   1975 

/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   1799         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1800 
-> 1801     plot_obj.generate()
   1802     plot_obj.draw()
   1803     return plot_obj.result

/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in generate(self)
    247     def generate(self):
    248         self._args_adjust()
--> 249         self._compute_plot_data()
    250         self._setup_subplots()
    251         self._make_plot()

/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in _compute_plot_data(self)
    365         if is_empty:
    366             raise TypeError('Empty {0!r}: no numeric data to '
--> 367                             'plot'.format(numeric_data.__class__.__name__))
    368 
    369         self.data = numeric_data

TypeError: Empty 'DataFrame': no numeric data to plot

Any ideas on how to deal with this error? It seems illogical, as the dtype of the variable i want to plot is float64 and therefore should contain numeric data to plot

1
Can you show geoinfect.head()? Btw, scheme = 'equal interval' is missing underscore: 'equal_interval'.martinfleis
Based on the error message, it seems that geoinfect is not a GeoDataFrame, but a DataFrame whose plot method is not specialized for geospatial data. Can you show how you read or created geoinfect ?joris
I just included some extra code on how i created the geodataframe, and i included the output of geoinfect.head()Nick Ho Sam Sooi

1 Answers

2
votes

You are trying to plot DataFrame, not GeoDataFrame as @joris mentioned. It is due to the wrong order of df-gdf in merge. Your GeoDataFrame needs to be left if you want to keep the result of a merge as GeoDataFrame.

geoinfect = geo.merge(inf, right_on = 'Country_Code', left_on = 'ADM0_A3')

Alternatively, you can reconstruct GeoDataFrame after the merge.

geoinfect = inf.merge(geo, left_on = 'Country_Code', right_on = 'ADM0_A3')
geoinfect = gpd.GeoDataFrame(geoinfect, crs=geo.crs)