admin管理员组文章数量:1278952
In Python-Polars, I am trying to get the shrinked data type of a column using an expression, to be able to run validations against it.
For example, I would like to build an expression that allows me to do the following:
df = pl.DataFrame({"list_column": [[1, 2], [3, 4], [5, 6]]})
shape: (3, 1)
┌─────────────┐
│ list_column │
│ --- │
│ list[i64] │
╞═════════════╡
│ [1, 2] │
│ [3, 4] │
│ [5, 6] │
└─────────────┘
df.select(type_check = pl.lit((pl.col("list_column").shrink_dtype() == pl.List)))
shape: (3, 2)
┌─────────────┬────────────┐
│ list_column ┆ type_check │
│ --- ┆ --- │
│ list[i64] ┆ bool │
╞═════════════╪════════════╡
│ [1, 2] ┆ true │
│ [3, 4] ┆ true │
│ [5, 6] ┆ true │
└─────────────┴────────────┘
Is this something feasible?
In Python-Polars, I am trying to get the shrinked data type of a column using an expression, to be able to run validations against it.
For example, I would like to build an expression that allows me to do the following:
df = pl.DataFrame({"list_column": [[1, 2], [3, 4], [5, 6]]})
shape: (3, 1)
┌─────────────┐
│ list_column │
│ --- │
│ list[i64] │
╞═════════════╡
│ [1, 2] │
│ [3, 4] │
│ [5, 6] │
└─────────────┘
df.select(type_check = pl.lit((pl.col("list_column").shrink_dtype() == pl.List)))
shape: (3, 2)
┌─────────────┬────────────┐
│ list_column ┆ type_check │
│ --- ┆ --- │
│ list[i64] ┆ bool │
╞═════════════╪════════════╡
│ [1, 2] ┆ true │
│ [3, 4] ┆ true │
│ [5, 6] ┆ true │
└─────────────┴────────────┘
Is this something feasible?
Share Improve this question asked Feb 24 at 16:53 yz_jcyz_jc 1897 bronze badges1 Answer
Reset to default 3No. In first place, the data type for the list_column in your example is pl.List(pl.Int64())
, so it would not be equal to pl.List
- polars has a strong distinction between different nested types, and shrink_dtype
does not currently works for that case at all.
Secondly, the data type is always the same for all rows within a given column, so it does not makes much sense to do the same operation for every single row.
You can use df.collect_schema()
to get a Schema object instead, which contains the data type for each column.
Alternatively, you might want to consider using dtype selectors if you wanted to perform different operations for each type.
本文标签: PythonPolars Get column type using an expressionStack Overflow
版权声明:本文标题:Python-Polars: Get column type using an expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741252602a2366074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论