将极坐标数据框与 dtype ENUM 的列连接起来

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

考虑有两个具有相同模式的

pl.DataFrame
。其中一列有
dtype=pl.Enum

import polars as pl

enum_col1 = pl.Enum(["type1"])
enum_col2 = pl.Enum(["type2"])
df1 = pl.DataFrame(
    {"enum_col": "type1", "value": 10},
    schema={"enum_col": enum_col1, "value": pl.Int64},
)
df2 = pl.DataFrame(
    {"enum_col": "type2", "value": 200},
    schema={"enum_col": enum_col2, "value": pl.Int64},
)

print(df1)
print(df2)

shape: (1, 2)
┌──────────┬───────┐
│ enum_col ┆ value │
│ ---      ┆ ---   │
│ enum     ┆ i64   │
╞══════════╪═══════╡
│ type1    ┆ 10    │
└──────────┴───────┘
shape: (1, 2)
┌──────────┬───────┐
│ enum_col ┆ value │
│ ---      ┆ ---   │
│ enum     ┆ i64   │
╞══════════╪═══════╡
│ type2    ┆ 200   │
└──────────┴───────┘

如果我尝试做一个简单的

pl.concat([df1, df2])
,我会收到以下错误:

polars.exceptions.SchemaError: type Enum(Some(local), Physical) is incompatible with expected type Enum(Some(local), Physical)

python python-polars
1个回答
0
投票
enum_col = enum_col1 | enum_col2

pl.concat([
    df.with_columns(pl.col.enum_col.cast(enum_col)) for df in [df1, df2]
])
shape: (2, 2)
┌──────────┬───────┐
│ enum_col ┆ value │
│ ---      ┆ ---   │
│ enum     ┆ i64   │
╞══════════╪═══════╡
│ type1    ┆ 10    │
│ type2    ┆ 200   │
└──────────┴───────┘
© www.soinside.com 2019 - 2024. All rights reserved.