8

I am processing shp files, and i'm having problems saving a geodataframe in a shp file.

import pandas as pd
import numpy as np
import os
import geopandas
location = '/home/braulio/Documents/example.shp'
datos = geopandas.read_file(location, encoding='UTF-8')

I don't have problems procesing data but when i try to save

A.to_file(r"/home/braulio/Documents/example2.shp")

and return an error:

ValueError                                Traceback (most recent call last)
<ipython-input-23-6a842789b4b4> in <module>
----> 1 A.to_file(r"/home/braulio/Documents/example2.shp")

~/anaconda3/envs/braulio/lib/python3.7/site-   packages/geopandas/geodataframe.py in to_file(self, filename, driver,   schema, **kwargs)
411         """
412         from geopandas.io.file import to_file
--> 413         to_file(self, filename, driver, schema, **kwargs)
414 
415     def to_crs(self, crs=None, epsg=None, inplace=False):

~/anaconda3/envs/braulio/lib/python3.7/site-packages/geopandas/io/file.py in to_file(df, filename, driver, schema,**kwargs)
109         with fiona.open(filename, 'w', driver=driver, crs=df.crs,
110                         schema=schema, **kwargs) as colxn:
--> 111             colxn.writerecords(df.iterfeatures())
112 
113 

~/anaconda3/envs/braulio/lib/python3.7/site-packages/fiona/collection.py in writerecords(self, records)
347         if self.mode not in ('a', 'w'):
348             raise IOError("collection not open for writing")
--> 349         self.session.writerecs(records, self)
350         self._len = self.session.get_length()
351         self._bounds = self.session.get_extent()

fiona/ogrext.pyx in fiona.ogrext.WritingSession.writerecs()

fiona/ogrext.pyx in fiona.ogrext.OGRFeatureBuilder.build()

ValueError: Invalid field type <class 'bytes'>
  • What are you doing with your data? Do you have the same problem saving datos data frame straight after reading it? It looks you are trying to save some type of data unsupported by Shapefile. – martinfleis Mar 18 '19 at 23:23
  • There is a column in your geodataframe with an invalid dtype (data type). It looks like the dtype `bytes` cannot be handled by geopandas or written to shapefiles. Check the dtype of your columns, and remove or transform that column. You can google "check dype dataframe columns" to get instructions on how to do it. – jberrio Mar 19 '19 at 00:01
  • After i check dtype for dataframe column, all the columns was type object. I delete one column of my shp and just work, but i don't really understand what happened. – Braulio Linares Mar 28 '19 at 22:27

3 Answers3

5

'bytes' and some other types such as 'list' are not supported data types for shapefiles. Here is the link to a github issue that discusses this problem.

I would recommend dropping the column that has 'bytes' type objects and then save the shapefile.

If that column is really important, change the value to 'string' type and then save the shapefile.

Waleed S Khan
  • 125
  • 1
  • 11
1

I resolve the problem changing the encoding to latin-1 in the begin

datos = geopandas.read_file(location, encoding='latin1')
0

The following worked in my case ("ValueError: Invalid field type <class 'numpy.int64'>"), hope that helps:

A = A.apply(pd.to_numeric, errors='ignore')
A = gpd.GeoDataFrame(A) # optional
A.set_geometry(col='geometry', inplace=True) # optional
A.to_file(path)
ConZZito
  • 325
  • 2
  • 5