为什么极坐标日期时间子集化很慢?

问题描述 投票:0回答:1

我正在寻找一种更快的方法来使用日期时间对极坐标数据帧进行子集化。我尝试了两种不同的方法,我相信有一种更快的方法,因为我也尝试过使用 pandas 并且它更快:

datetime.datetime(2009, 8, 3, 0, 0)
datetime.datetime(2009, 11, 3, 0, 0)


# - 1st
%%timeit
df_window = df_stock.filter(
    (df_stock["date_time"] >= start_date)
    & (df_stock["date_time"] <= end_date)
)
# 2.61 ms ± 243 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# - 2nd
%%timeit
df_window = df_stock.filter(
    (pl.col("date_time") >= start_date) & (pl.col("date_time") <= end_date)
)
# 12.5 ms ± 801 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Pandas 的日期时间子集化速度更快。

df_test = df_stock.to_pandas()
df_test.set_index('date_time', inplace=True)

%%timeit
df_test['2009-8-3':'2009-8-3']
# 1.02 ms ± 108 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

那么为什么在这种情况下熊猫比极地更快呢?是否有更快的极地日期时间子集形式?

环境:

  • Spyder 版本:5.4.0(conda)
  • Python版本:3.8.13 64位
  • 操作系统:Linux 5.15.79.1-microsoft-standard-WSL2
python pandas dataframe python-polars
1个回答
3
投票

这是否只是对相对较小的查询进行基准测试的问题? 我的基准测试显示完全相反。

数据

让我们从大量数据开始,以便子集化过程代表工作的最大份额。 我还将

shuffle
数据,这样就不会涉及“排序”问题。

import time
import polars as pl
from datetime import datetime

df_stock = pl.DataFrame(
    {
        "date_time": pl.datetime_range(
            start=datetime(2000, 1, 1, 0, 0),
            end=datetime(2023, 1, 1, 0, 0),
            interval="1s",
            eager=True,
        ).shuffle(seed=0)
    }
)
df_stock
shape: (725_846_401, 1)
┌─────────────────────┐
│ date_time           │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2022-01-02 21:46:35 │
│ 2003-12-16 18:57:19 │
│ 2018-06-20 14:31:08 │
│ 2020-04-09 07:18:38 │
│ …                   │
│ 2013-07-12 00:39:09 │
│ 2021-09-08 17:18:59 │
│ 2010-08-18 16:00:18 │
│ 2010-02-06 05:13:05 │
└─────────────────────┘

7.25 亿条记录应该是一个好的开始。

测试用例

我已将您代码中的三种情况放在单独的测试中。

测试#1

start_date = datetime(2009, 8, 3, 0, 0)
end_date = datetime(2009, 11, 3, 0, 0)

start = time.perf_counter()
_ = df_stock.filter(
    (df_stock["date_time"] >= start_date) & (df_stock["date_time"] <= end_date)
)
print(time.perf_counter() - start)

测试#2

start = time.perf_counter()
_ = df_stock.filter(
    (pl.col("date_time") >= start_date) & (pl.col("date_time") <= end_date)
)
print(time.perf_counter() - start)

测试#3

df_test = df_stock.to_pandas()
df_test.set_index("date_time", inplace=True)
start = time.perf_counter()
_ = df_test["2009-8-3":"2009-11-3"]
print(time.perf_counter() - start)

以下是在我的系统(32 核系统)上运行上述 3 次的时间。

测试#1:1.1206、1.1199、1.1202

测试#2:0.8342、0.8215、0.8227

测试#3:4.6082、4.5932、4.6185

在我的系统上,Pandas 索引运行速度是迄今为止最慢的。 我可以运行超过三遍,但我认为模式非常清晰。

您可以在您的系统上运行更大的测试吗? 也许这里还有其他东西在起作用......

供参考,我的测试环境的版本信息...

---Version info---
Polars: 0.15.7
Index type: UInt32
Platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
Python: 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]
---Optional dependencies---
pyarrow: 10.0.1
pandas: 1.5.2
numpy: 1.24.0
fsspec: 2022.11.0
connectorx: 0.3.1
xlsx2csv: 0.8
matplotlib: 3.6.2
© www.soinside.com 2019 - 2024. All rights reserved.