'geodataframe.to_file Invalid field type <class 'bytes'>
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'>
Solution 1:[1]
'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.
Solution 2:[2]
I resolve the problem changing the encoding to latin-1 in the begin
datos = geopandas.read_file(location, encoding='latin1')
Solution 3:[3]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Waleed S Khan |
| Solution 2 | Mauricio Velasco |
| Solution 3 | ConZZito |
