如何加快大数据创建Point GeoSeries的速度?

Xin*_*ang 0 python pandas shapely geopandas

我有两个一维数组,想将它们组合成一个 Point GeoSeries,如下所示:

import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point

x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))
GeoSeries(map(Point, zip(x, y)))
Run Code Online (Sandbox Code Playgroud)

在我的笔记本电脑上大约需要 5 秒。是否可以加速GeoSeries的生成?

小智 5

map加速此过程,您需要使用向量化运算,而不是使用 。points_from_xyGeoPandas 提供的功能专门为此目的进行了优化。这是在我的机器上运行的示例:

import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point
import geopandas as gpd
import time

x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))

s = time.time()

GeoSeries(map(Point, zip(x, y)))

f = time.time()
print("time elapsed with `map` : ", f - s)

geo_series = gpd.GeoSeries(gpd.points_from_xy(x, y))

print("time elapsed with `points_from_xy` : ", time.time() - f)
Run Code Online (Sandbox Code Playgroud)

输出:

time elapsed with `map` :  9.318699359893799
time elapsed with `points_from_xy` :  0.654371976852417
Run Code Online (Sandbox Code Playgroud)

看,points_from_xy由于使用了矢量化方法,速度几乎快了 10 倍。

从此处查看geopandas.points_from_xy文档以了解更多信息:https ://geopandas.org/en/stable/docs/reference/api/geopandas.points_from_xy.html