! echo $GDS_ENV_VERSION
9.0
from libpysal import weights
import pandas
import geopandas
import numpy as np
import polars as pl
Utilities:
def lag_spatial_adj(w_adj, y):
return (
w_synth_adj
.join(y_synth, on='focal')
.eval('product = weight * y_synth')
.groupby('focal')
['product']
.mean()
)
def lag_spatial_adj_polars(w_adj_pl, y_pl):
return (
w_adj_pl
.join()
)
w_synth = weights.lat2W(1000, 1000, 'queen')
w_synth.transform = 'R'
w_synth_adj = w_synth.to_adjlist()
w_synth_adj_pl = pl.DataFrame(w_synth_adj)
y_synth = pandas.Series(np.random.random(1000000), name='y_synth')
y_synth_pl = pl.Series(y_synth)
_ = weights.lag_spatial(w_synth, y)
%timeit pysal_lag = weights.lag_spatial(w_synth, y)
4.02 ms ± 39.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
pandas
%timeit pandas_lag = lag_spatial_adj(w_synth_adj, y_synth)
299 ms ± 7.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
polars
geopandas.overlay
¶