Skip to content

Error when loading an empty shapefile #649

@WeatherGod

Description

@WeatherGod

as of v0.3.0, when loading a valid shapefile that has no records, an error is raised:

import geopandas as gpd
df = gpd.read_file('active_perimeters_dd83.shp')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/broot/scratch/miniconda/lib/python2.7/site-packages/geopandas/io/file.py", line 30, in read_file
    gdf = gdf[columns]
  File "/home/broot/scratch/miniconda/lib/python2.7/site-packages/geopandas/geodataframe.py", line 398, in __getitem__
    result = super(GeoDataFrame, self).__getitem__(key)
  File "/home/broot/scratch/miniconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2133, in __getitem__
    return self._getitem_array(key)
  File "/home/broot/scratch/miniconda/lib/python2.7/site-packages/pandas/core/frame.py", line 2177, in _getitem_array
    indexer = self.loc._convert_to_indexer(key, axis=1)
  File "/home/broot/scratch/miniconda/lib/python2.7/site-packages/pandas/core/indexing.py", line 1269, in _convert_to_indexer
    .format(mask=objarr[mask]))
KeyError: "[u'IRWINID' u'MAPMETHOD' u'UNITIDPROT' u'UNITIDOWN' u'INCIDENTID'\n u'FIRENAME' u'PERDATTIME' u'COMMENTS' u'AGENCY' u'ACTIVE' u'COMPPARID'\n u'FIREYEAR' u'DATECRNT' u'INCIWEBID' u'FIRECODE' u'COMPFIRECD' u'FIRENUM'\n u'COMPLEXNM' u'STATE' u'INCOMPLEX' u'GISACRES' u'MERGEID' 'geometry'] not in index"

Empty shapefiles are perfectly valid in my processing, as the shapefile is ingested daily to process the polygons of any active wildfires. At the moment, there are no active wildfires in the US with polygons defined.

In v0.2.1, there was a different, but still undesirable behavior:

>>> import geopandas as gpd
>>> df = gpd.read_file('active_perimeters_dd83.shp')
>>> df
Empty GeoDataFrame
Columns: []
Index: []

The shapefile has columns defined, and fiona can see those columns just fine, too. But geopandas just silently ignores all of that. This is undesirable because one of my processing steps is to validate the schema of the input shapefile. If there is no columns, then I don't know if that is because there is no data, or because the wrong input file was selected.
active_perimeters_dd83.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions