我正在try 将我的一些pandas代码切换为polars以利用其性能.我发现.滤镜操作比使用. loco的类似切片慢得多.
import pandas as pd
import polars as pl
import datetime as dt
import numpy as np
date_index = pd.date_range(dt.date(2001,1,1), dt.date(2020,1,1),freq='1H')
n = date_index.shape[0]
test_pd = pd.DataFrame(data = np.random.randint(1,100, n), index=date_index, columns = ['test'])
test_pl = pl.DataFrame(test_pd.reset_index())
test_dates = date_index[np.random.randint(0,n,1000)]
st = time.perf_counter()
for i in test_dates:
d = test_pd.loc[i,:]
print(f"Pandas {time.perf_counter() - st}")
st = time.perf_counter()
for i in test_dates:
d = test_pl.filter(index=i)
print(f"Polars {time.perf_counter() - st}")
Pandas 0.1854726000019582
Polars 2.1125728000042727
还有其他方法可以加速两极的切片操作吗?