5

I am looking for the equivalent argument to pandas.read_csv(index_col='column') in the geopandas.read_file() function. My goal is to read a shapefile and specify the index column. However, the following does not set any index column and returns the same GeoDataframe as calling the function without the argument:

import geopandas as gpd

gdf = gpd.read_file('test.shp', index_col='column')

The documentation of the read_file function does not list index_col as an argument and states that one can specify keyword arguments that are going to be passed to the fiona.open function (but not to any pandas.read_* function).

A workaround is to use the set_index() method after reading the file:

import geopandas as gpd

gdf = gpd.read_file('test.shp')
gdf = gdf.set_index('column')

# or

gdf = gpd.read_file('test.shp').set_index('column')

However, it feels like specifying the index should be possible directly from the read_file function.

2
  • It is not. Your workaround with set_index() is the best option so far. Commented May 17, 2020 at 17:19
  • Feel free to post your comment as an answer and I will accept it. Commented May 17, 2020 at 17:32

1 Answer 1

6

GeoPandas does not have such an option (as of 0.7.0 and likely 0.8.0), so the workaround with .set_index is the best option. If it did have the option, it would likely just do the same under the hood, so there will be no difference anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.