You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按symbol分组找出多个pl.DataFrame的共通日期并过滤数据?

按Symbol分组筛选多DataFrame的共通日期行

我有数量不固定的Polars DataFrame,它们共享symbol和date列,每个DataFrame还包含若干无关的额外列。所有DataFrame的symbol列内容完全一致(每个symbol值都存在于所有数据框中),但date列的日期并非在所有DataFrame中完全重叠。需要按symbol分组,找出每个组在所有DataFrame中的共通日期,再用这些日期对每个DataFrame进行过滤。

示例DataFrame

import polars as pl

df1 = pl.from_repr("""
┌────────┬────────────┬────────────────┐
│ symbol ┆ date       ┆ some_other_col │
│ ---    ┆ ---        ┆ ---            │
│ str    ┆ str        ┆ i64            │
╞════════╪════════════╪════════════════╡
│ AAPL   ┆ 2023-01-01 ┆ 0              │
│ AAPL   ┆ 2023-01-02 ┆ 1              │
│ AAPL   ┆ 2023-01-03 ┆ 2              │
│ AAPL   ┆ 2023-01-04 ┆ 3              │
│ GOOGL  ┆ 2023-01-02 ┆ 4              │
│ GOOGL  ┆ 2023-01-03 ┆ 5              │
│ GOOGL  ┆ 2023-01-04 ┆ 6              │
└────────┴────────────┴────────────────┘
""")

df2 = pl.from_repr("""
┌────────┬────────────┬─────────────┐
│ symbol ┆ date       ┆ another_col │
│ ---    ┆ ---        ┆ ---         │
│ str    ┆ str        ┆ i64         │
╞════════╪════════════╪═════════════╡
│ AAPL   ┆ 2023-01-02 ┆ 0           │
│ AAPL   ┆ 2023-01-03 ┆ 1           │
│ AAPL   ┆ 2023-01-04 ┆ 2           │
│ GOOGL  ┆ 2023-01-01 ┆ 3           │
│ GOOGL  ┆ 2023-01-02 ┆ 4           │
│ GOOGL  ┆ 2023-01-03 ┆ 5           │
│ GOOGL  ┆ 2023-01-04 ┆ 6           │
│ GOOGL  ┆ 2023-01-05 ┆ 7           │
└────────┴────────────┴─────────────┘
""")

df3 = pl.from_repr("""
┌────────┬────────────┬──────────┐
│ symbol ┆ date       ┆ some_col │
│ ---    ┆ ---        ┆ ---      │
│ str    ┆ str        ┆ i64      │
╞════════╪════════════╪══════════╡
│ AAPL   ┆ 2023-01-02 ┆ 0        │
│ AAPL   ┆ 2023-01-03 ┆ 1        │
│ AAPL   ┆ 2023-01-04 ┆ 2        │
│ AAPL   ┆ 2023-01-05 ┆ 3        │
│ GOOGL  ┆ 2023-01-03 ┆ 4        │
│ GOOGL  ┆ 2023-01-04 ┆ 5        │
└────────┴────────────┴──────────┘
""")

每个Symbol对应的共通日期

  • AAPL:["2023-01-02", "2023-01-03", "2023-01-04"]
  • GOOGL:["2023-01-03", "2023-01-04"]

过滤后的预期结果

过滤后的DataFrame 1

shape: (5, 3)
┌────────┬────────────┬────────────────┐
│ symbol ┆ date       ┆ some_other_col │
│ ---    ┆ ---        ┆ ---            │
│ str    ┆ str        ┆ i64            │
╞════════╪════════════╪════════════════╡
│ AAPL   ┆ 2023-01-02 ┆ 1              │
│ AAPL   ┆ 2023-01-03 ┆ 2              │
│ AAPL   ┆ 2023-01-04 ┆ 3              │
│ GOOGL  ┆ 2023-01-03 ┆ 5              │
│ GOOGL  ┆ 2023-01-04 ┆ 6              │
└────────┴────────────┴────────────────┘

过滤后的DataFrame 2

shape: (5, 3)
┌────────┬────────────┬─────────────┐
│ symbol ┆ date       ┆ another_col │
│ ---    ┆ ---        ┆ ---         │
│ str    ┆ str        ┆ i64         │
╞════════╪════════════╪═════════════╡
│ AAPL   ┆ 2023-01-02 ┆ 0           │
│ AAPL   ┆ 2023-01-03 ┆ 1           │
│ AAPL   ┆ 2023-01-04 ┆ 2           │
│ GOOGL  ┆ 2023-01-03 ┆ 5           │
│ GOOGL  ┆ 2023-01-04 ┆ 6           │
└────────┴────────────┴─────────────┘

过滤后的DataFrame 3

shape: (5, 3)
┌────────┬────────────┬──────────┐
│ symbol ┆ date       ┆ some_col │
│ ---    ┆ ---        ┆ ---      │
│ str    ┆ str        ┆ i64      │
╞════════╪════════════╪══════════╡
│ AAPL   ┆ 2023-01-02 ┆ 0        │
│ AAPL   ┆ 2023-01-03 ┆ 1        │
│ AAPL   ┆ 2023-01-04 ┆ 2        │
│ GOOGL  ┆ 2023-01-03 ┆ 4        │
│ GOOGL  ┆ 2023-01-04 ┆ 5        │
└────────┴────────────┴──────────┘

内容的提问来源于stack exchange,提问作者Andi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 20:55:56