55except ModuleNotFoundError : # pragma: no cover
66 raise RuntimeError (
77 'The `pandas_types` module requires "pandas" to be installed. '
8- " You can install it with \ " pip install 'pydantic-extra-types[pandas]' \" ."
8+ ' You can install it with "pip install \ ' pydantic-extra-types[pandas]\' ".'
99 )
1010
1111from typing import Any , ClassVar
@@ -24,9 +24,11 @@ class Series(pd.Series): # type: ignore[misc]
2424 from pydantic import BaseModel
2525 from pydantic_extra_types.pandas_types import Series
2626
27+
2728 class MyModel(BaseModel):
2829 values: Series[int]
2930
31+
3032 model = MyModel(values=[1, 2, 3])
3133 print(model.values.tolist()) # [1, 2, 3]
3234 ```
@@ -38,9 +40,7 @@ def __class_getitem__(cls, item: type) -> type: # type: ignore[override]
3840 return type (f'Series[{ item .__name__ } ]' , (cls ,), {'_item_type' : item })
3941
4042 @classmethod
41- def __get_pydantic_core_schema__ (
42- cls , source : type [Any ], handler : GetCoreSchemaHandler
43- ) -> core_schema .CoreSchema :
43+ def __get_pydantic_core_schema__ (cls , source : type [Any ], handler : GetCoreSchemaHandler ) -> core_schema .CoreSchema :
4444 if cls ._item_type is not None :
4545 item_schema = handler .generate_schema (cls ._item_type )
4646 else :
@@ -85,9 +85,11 @@ class Index:
8585 from pydantic import BaseModel
8686 from pydantic_extra_types.pandas_types import Index
8787
88+
8889 class MyModel(BaseModel):
8990 idx: Index[str]
9091
92+
9193 model = MyModel(idx=['a', 'b', 'c'])
9294 print(model.idx.tolist()) # ['a', 'b', 'c']
9395 ```
@@ -99,9 +101,7 @@ def __class_getitem__(cls, item: type) -> type:
99101 return type (f'Index[{ item .__name__ } ]' , (cls ,), {'_item_type' : item })
100102
101103 @classmethod
102- def __get_pydantic_core_schema__ (
103- cls , source : type [Any ], handler : GetCoreSchemaHandler
104- ) -> core_schema .CoreSchema :
104+ def __get_pydantic_core_schema__ (cls , source : type [Any ], handler : GetCoreSchemaHandler ) -> core_schema .CoreSchema :
105105 if cls ._item_type is not None :
106106 item_schema = handler .generate_schema (cls ._item_type )
107107 else :
@@ -148,13 +148,16 @@ class DataFrame:
148148 from pydantic import BaseModel
149149 from pydantic_extra_types.pandas_types import DataFrame
150150
151+
151152 class MySchema(TypedDict):
152153 name: str
153154 age: int
154155
156+
155157 class MyModel(BaseModel):
156158 people: DataFrame[MySchema]
157159
160+
158161 model = MyModel(people={'name': ['Alice', 'Bob'], 'age': [30, 25]})
159162 print(model.people)
160163 ```
@@ -166,15 +169,11 @@ def __class_getitem__(cls, schema_cls: type) -> type:
166169 return type (f'DataFrame[{ schema_cls .__name__ } ]' , (cls ,), {'_schema_cls' : schema_cls })
167170
168171 @classmethod
169- def __get_pydantic_core_schema__ (
170- cls , source : type [Any ], handler : GetCoreSchemaHandler
171- ) -> core_schema .CoreSchema :
172+ def __get_pydantic_core_schema__ (cls , source : type [Any ], handler : GetCoreSchemaHandler ) -> core_schema .CoreSchema :
172173 if cls ._schema_cls is not None :
173174 annotations = cls ._schema_cls .__annotations__
174175 fields = {
175- col : core_schema .typed_dict_field (
176- core_schema .list_schema (handler .generate_schema (col_type ))
177- )
176+ col : core_schema .typed_dict_field (core_schema .list_schema (handler .generate_schema (col_type )))
178177 for col , col_type in annotations .items ()
179178 }
180179 inner_schema : core_schema .CoreSchema = core_schema .typed_dict_schema (fields )
0 commit comments