pandera.engines.pandas_engine.PythonTypedDict

class pandera.engines.pandas_engine.PythonTypedDict(special_type=None)[source]

一种支持python泛型的数据类型。

属性

auto_coerce

在所有情况下是否强制转换为True

coercion_model

continuous

数字数据类型是否是连续的。

generic_type

special_type

方法

__init__(special_type=None)[source]
check(pandera_dtype, data_container=None)[source]

检查数据容器是否具有预期类型。

Return type:

Union[bool, Iterable[bool]]

coerce(data_container)[source]

将数据容器强制转换为指定的数据类型。

Return type:

Union[Series, DataFrame]

coerce_value(value)[source]

将一个值强制转换为特定类型。

Return type:

Any

try_coerce(data_container)[source]

强制数据容器转换为数据类型, 如果强制转换失败,则引发 ParserError :raises: ParserError: 如果强制转换失败

Return type:

Union[Series, DataFrame]

type(fields=None, /, *, total=True, **kwargs)[source]

一个简单的类型化命名空间。在运行时,它等同于一个普通的字典。

TypedDict 创建了一种字典类型,使得类型检查器将期望所有实例具有一定的键集合,每个键都与一致类型的值相关联。这个期望在运行时不会被检查。

用法:

>>> class Point2D(TypedDict):
...     x: int
...     y: int
...     label: str
...
>>> a: Point2D = {'x': 1, 'y': 2, 'label': 'good'}  # OK
>>> b: Point2D = {'z': 3, 'label': 'bad'}           # Fails type check
>>> Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
True

类型信息可以通过 Point2D.__annotations__ 字典访问,以及 Point2D.__required_keys__ 和 Point2D.__optional_keys__ 冻结集合。TypedDict 支持一种额外的等效形式:

Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})

默认情况下,所有键必须在TypedDict中存在。可以通过指定完全性来覆盖此行为:

class Point2D(TypedDict, total=False):
    x: int
    y: int

这意味着一个 Point2D TypedDict 可以省略任何键。类型检查器只需要支持字面量 False 或 True 作为 total 参数的值。True 是默认值,并且使所有在类体中定义的项都是必需的。

Required 和 NotRequired 特殊形式也可以用来标记单个键是必需的还是不必需的:

class Point2D(TypedDict):
    x: int               # the "x" key must always be present (Required is the default)
    y: NotRequired[int]  # the "y" key can be omitted

有关必需和非必需的更多详细信息,请参见PEP 655。

__call__(data_container)[source]

将数据容器强制转换为数据类型。