Home > Mobile >  AttributeError: 'pandas' object has no attribute 'gen'
AttributeError: 'pandas' object has no attribute 'gen'

Time:04-02

I am pretty new in programming. I am trying to get graphs from polygons from a shp-file. This code runs with another file. But in this file, some of the graph-names don't work. An error occurs that the 'pandas' object has no attribute 'gen'.

    import geopandas as gpd
    import osmnx as ox
    path_to_folder = r'/Users/xxx/Desktop/Example/cities.shp'
    cities = gpd.read_file('/Users/xxx/Desktop/Example/cities.shp')
    
    for item in cities.itertuples():
        print(item.gen)
        graph = ox.graph_from_polygon(item.geometry)
        ox.save_graphml(graph, filepath = '/Users/xxx/Desktop/'   item.gen   '.graphml')
    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [13], in <cell line: 1>()
      1 for item in cities.itertuples():
----> 2     print(item.gen)
      3     graph = ox.graph_from_polygon(item.geometry)
      4     ox.save_graphml(graph, filepath = '/Users/xxx/Desktop/'   item.gen   '.graphml')

AttributeError: 'Pandas' object has no attribute 'gen'

I tried to fix it with 'replace' or 'join' the false examples, but actually don't know how to incorporate it in the code. Does anyone has an idea?

CodePudding user response:

you have an extra = on the variable cities declaration.

Right now you have:

cities = gpd.read_file =('/Users/xxx/Desktop/Example/cities.shp')

That creates a variable cities, equal to a variable gpd.read_file, equal to a string with the value '/Users/xxx/Desktop/Example/cities.shp'

just remove the second equal so you call the function gpd.read_file with the string as parameter.

cities = gpd.read_file('/Users/xxx/Desktop/Example/cities.shp')

CodePudding user response:

  • as per comment, you are using itertuples() that returns named tuples. This means your code needs to consider situations where a name does not exist
name = item.gen if "gen" in item._fields else item.Index
  • have generated a full code MWE example of this. I really recommend using pathlib instead of hardcoded paths to files.
import geopandas as gpd
import osmnx as ox
from pathlib import Path

# use pathlib - better practice
p = Path.home().joinpath("Desktop/Examples")
if not p.exists():
    p.mkdir()

# path_to_folder = r'/Users/xxx/Desktop/Example/cities.shp'
# cities = gpd.read_file('/Users/xxx/Desktop/Example/cities.shp')
cities = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))

# need polygons - not really relevant to question
cities["NAME"] = cities["name"].str.upper()
cities_polys = gpd.read_file(
    "https://github.com/drei01/geojson-world-cities/raw/master/cities.geojson"
)
cities = (
    cities.drop(columns=["geometry"])
    .merge(cities_polys, on="NAME")
    .drop_duplicates(subset=["NAME"])
    .reset_index(drop=True)
    .head(5)
)
cities["gen"] = cities["NAME"]
# end of code not relevant to question

for item in cities.itertuples():
    name = item.gen if "gen" in item._fields else item.Index
    print(name)
    graph = ox.graph_from_polygon(item.geometry)
    ox.save_graphml(graph, filepath=p.joinpath(str(name)   ".graphml"))
  • Related