*** TypeError: Expected String or Unicode when loading SHP to Spatially-Enabled Data Frame

1459
2
Jump to solution
05-06-2023 05:36 PM
DarrenConly
Occasional Contributor

I'm trying to load a very wide (145 fields) feature class, and to save memory, I want to only load in 6 of the fields. Following the documentation for the from_featureclass() method, I tried specifying the fields I wanted using the fields parameter.

However, when I specify the fields parameter, I get the error *** TypeError: Expected String or Unicode.

Then, if I remove the fields parameter, the error goes away and the feature class loads successfully.

Has anyone else had this issue? Example code is below. *Notably*, I have tried both with a shapefile and with a feature class in a FGDB, and get the same error with both file types

 

# fields I want to include in SEDF
fields2use = ['A', 'B', 'CAPCLASS', 'LANES', 'SHAPE', 'SPEED']

# *Successful* loading when NOT specifying the fields parameter
sedf_hwy = pd.DataFrame.spatial.from_featureclass(self.fc_daynet, where_clause=where_sql, sr=self.crs_sacog)

# *Failed* loading when trying to load while specifying fields parameter.
sedf_hwy = pd.DataFrame.spatial.from_featureclass(self.fc_daynet, fields=fields2use, where_clause=where_sql, sr=self.crs_sacog)
# result: *** TypeError: Expected String or Unicode

 

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Have you tried without explicitly requesting the SHAPE field?

# fields I want to include in SEDF
fields2use = ['A', 'B', 'CAPCLASS', 'LANES', 'SPEED']

 

View solution in original post

2 Replies
Luke_Pinner
MVP Regular Contributor

Have you tried without explicitly requesting the SHAPE field?

# fields I want to include in SEDF
fields2use = ['A', 'B', 'CAPCLASS', 'LANES', 'SPEED']

 

DarrenConly
Occasional Contributor

Thanks @Luke_Pinner! That fixed it. I'm used to filtering columns after completely loading the dataframe, so if you don't specify the SHAPE field it removes the geometry from the df. But since fields are specified before creating the spatial dataframe, it makes sense you shouldn't be specifying it.

That said ESRI **really** should make the error message clearer. E.g., instead of saying some generic TypeError message, the error should say something like "field 'SHAPE' not found in source file" or something like that.