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

Polars >=v1版本中如何检查数据类型是否符合选择器条件?

在Polars v1+中检查数据类型是否属于数值/时间类型的替代方案

旧版本(<v1)的实现方式

在Polars v1版本之前,可直接通过pl.selectors导出的类型列表判断数据类型归属:

import polars as pl

# 验证UInt8是否为数值类型
assert pl.UInt8 in pl.selectors.NUMERIC_DTYPES
# 验证UInt8是否非时间类型
assert pl.UInt8 not in pl.selectors.TEMPORAL_DTYPES

v1+版本的问题

从Polars v1开始,NUMERIC_DTYPES、TEMPORAL_DTYPES这类类型列表不再对外公开导出,直接引用会触发Pyright/Pylance的私有导入报错:

"TEMPORAL_DTYPES" is not exported from module "polars.selectors" (Pylance reportPrivateImportUsage)

推荐替代方案

方案1:使用数据类型自带的公开判断方法

这是Polars官方提供的稳定API,写法简洁直接:

import polars as pl

# 判断UInt8是否为数值类型
assert pl.UInt8.is_numeric()
# 判断UInt8是否为时间类型
assert not pl.UInt8.is_temporal()

方案2:基于Selector API实现类似contains的逻辑

若需贴合你期望的selector调用风格,可访问selector实例的私有_dtypes属性(注意:私有属性可能随版本迭代变更):

import polars as pl

assert pl.UInt8 in pl.selectors.numeric()._dtypes
assert pl.UInt8 not in pl.selectors.temporal()._dtypes

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 08:52:04