将numpy数组转换为Shapely点的最有效方法是什么?

时间:2018-06-21 15:10:20

标签: python pandas numpy shapely geopandas

我有一个函数将点的网格输出为x和y numpy数组以进行插值,但是在插值之前,我想使用Geopandas与研究边界进行交点(否则,一半的插值点落入了海洋)。

我正在生成这样的观点:

import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import Point

x = np.linspace(0,100,100)
y = np.linspace(0,100,100)
x, y = np.meshgrid(x, y)
x, y = x.flatten(), y.flatten()


f, ax = plt.subplots()

plt.scatter(x, y)
plt.axis('equal')
plt.show()

是否存在将这些numpy数组转换为shapely.Point([x, y])的有效方法,以便可以将它们放置在geopandas地理数据框中?

这是我目前的方法:

interp_points = []
index = 0
y_list = yi.tolist()
for x in xi.tolist():
    interp_points.append(Point(x,y_list[index]))
    index += 1

但是似乎转换为列表然后进行迭代可能不是提高性能的好方法,并且我大约有160,000分。

3 个答案:

答案 0 :(得分:1)

没有shapely内置的方法,因此您需要自己遍历这些值。为此,这应该是一种相当有效的方法:

In [4]: from geopandas import GeoSeries

In [5]: s = GeoSeries(map(Point, zip(x, y)))

In [6]: s.head()
Out[6]: 
0                    POINT (0 0)
1     POINT (1.01010101010101 0)
2     POINT (2.02020202020202 0)
3     POINT (3.03030303030303 0)
4    POINT (4.040404040404041 0)
dtype: object

In [6]: %timeit GeoSeries(map(Point, zip(x, y)))
114 ms ± 8.45 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

(或稍作选择的GeoSeries(list(zip(x, y))).map(Point)

请参见此处的一些示例:http://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html

有一些(固定的)工作可以将其直接包含在大熊猫中:https://github.com/geopandas/geopandas/pull/75

答案 1 :(得分:1)

我认为这是一个好方法:

#create a list from the column 'ticker'
my_tickers = my_pf['ticker'].tolist()

#collect the opening data per ticker
for ticker in my_tickers:
    open_price = yf.Ticker(ticker).info.get('open')
    
    print(ticker, open_price)

答案 2 :(得分:0)

最好使用此列表理解:

[在arr.tolist()中x的元组(x)]

相关问题