I have a GeoPandas DataFrame with a polygon in each row. I want to add a column with coordinates of a bounding box of each polygon. I can do it this way:
def create_bbox(row): xmin, ymin, xmax, ymax = row.geometry.bounds return Polygon.from_bounds(xmin, ymin, xmax, ymax) osm_buildings['bbox'] = osm_buildings.apply(lambda row: create_bbox(row), axis=1)
However, because of the size of the dataset, I need to speed this process up. I want to use vectorization. What I've tried is this:
osm_buildings['bbox'] = Polygon.from_bounds( osm_buildings.geometry.bounds.minx, osm_buildings.geometry.bounds.miny, osm_buildings.geometry.bounds.maxx, osm_buildings.geometry.bounds.maxy)
However, I get
*** ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I don't understand very well what "truth value" is the error referring to and how can I fix it.
Question: How can I make this operation faster than using
apply()? I am asking specifically about vectorization but if there is something else to speed it up I'm interested as well.