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.

read_file on an empty layer in a gdb results in a KeyError

See original GitHub issue

I’m not sure if this is expected behavior, but I was trying to read a specific layer in a geodatabase which happened to be empty (has no rows, but has columns) using geopandas.read_file() and it resulted in a KeyError. The same gdb and the layer within the gdb can be obtained from here: https://coast.noaa.gov/htdata/Inundation/SLR/SLRdata/Pacific/HI_Lanai_slr_data_dist.zip

Full stack trace below:

gdf = geopandas.read_file('../noaa/sea_level_rise/hawaii/HI_Lanai_slr_data_dist/HI_Lanai_slr_final_dist.gdb', layer='HI_Lanai_low_2ft')

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-513ea4fddcc2> in <module>()
      1 gdf = geopandas.read_file('../noaa/sea_level_rise/hawaii/HI_Lanai_slr_data_dist/HI_Lanai_slr_final_dist.gdb',
----> 2                          layer='HI_Lanai_low_2ft')

/usr/local/lib/python3.6/site-packages/geopandas/io/file.py in read_file(filename, **kwargs)
     28         # re-order with column order from metadata, with geometry last
     29         columns = list(f.meta["schema"]["properties"]) + ["geometry"]
---> 30         gdf = gdf[columns]
     31 
     32     return gdf

/usr/local/lib/python3.6/site-packages/geopandas/geodataframe.py in __getitem__(self, key)
    396         GeoDataFrame.
    397         """
--> 398         result = super(GeoDataFrame, self).__getitem__(key)
    399         geo_col = self._geometry_column_name
    400         if isinstance(key, string_types) and key == geo_col:

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2051         if isinstance(key, (Series, np.ndarray, Index, list)):
   2052             # either boolean or fancy integer index
-> 2053             return self._getitem_array(key)
   2054         elif isinstance(key, DataFrame):
   2055             return self._getitem_frame(key)

/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_array(self, key)
   2095             return self.take(indexer, axis=0, convert=False)
   2096         else:
-> 2097             indexer = self.ix._convert_to_indexer(key, axis=1)
   2098             return self.take(indexer, axis=1, convert=True)
   2099 

/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1228                 mask = check == -1
   1229                 if mask.any():
-> 1230                     raise KeyError('%s not in index' % objarr[mask])
   1231 
   1232                 return _values_from_object(indexer)

KeyError: "['Id' 'grid_code' 'Shape_Length' 'Shape_Area' 'geometry'] not in index"

Are there any work arounds? or do I have to try-except the KeyError which seems a little broad.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kuanbcommented, Apr 4, 2018

This is hacky, and does not solve the problem, but might at least improve your KeyError try .. catch logic:

try:
    gdf = gpd.read_file(filepath)
except KeyError as exception:
    with fiona.open(dir_data, driver="OpenFileGDB") as src:
        if _ensure_that_there_are_zero_rows(src):
            pass
        else:
            raise exception

With this method, you would at least be able to try a second attempt at reading the GDB and double checking that there indeed are no rows.

1reaction
jorisvandenbosschecommented, Apr 4, 2018

We recently fixed a bug for reading empty files (https://github.com/geopandas/geopandas/issues/649, PR https://github.com/geopandas/geopandas/pull/653), I suppose this will be the same.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I stop a process if results of a layer is empty?
I have script that creates a layer but sometimes the end result of that later returns no records. How do I tell Python...
Read more >
Reading the names of geodatabase file layers in Python
The fiona.listlayers() function returns a list of names of layers in a dataset. import fiona fiona.listlayers('NYCFutureHighTideWithSLR.gdb').
Read more >
Changelog — Python 3.11.1 documentation
gh-91447: Fix findtext in the xml module to only give an empty string when the text ... The f_lasti attribute of Python-layer frame...
Read more >
Bug listing with status RESOLVED with resolution FIXED as at ...
... Bug:624 - "emerge removes empty dirs when updating a packages! ... Bug:1793 - "gdb build error" status:RESOLVED resolution:FIXED severity:normal ...
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