-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Implement Pydantic V2 protocol #54034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
Union, | ||
cast, | ||
overload, | ||
Type | ||
) | ||
import warnings | ||
import weakref | ||
|
@@ -37,6 +38,7 @@ | |
) | ||
from pandas._libs.lib import is_range_indexer | ||
from pandas.compat import PYPY | ||
from pandas.compat._optional import import_optional_dependency | ||
from pandas.compat.numpy import function as nv | ||
from pandas.errors import ( | ||
ChainedAssignmentError, | ||
|
@@ -148,6 +150,10 @@ | |
import pandas.plotting | ||
|
||
if TYPE_CHECKING: | ||
import pydantic_core | ||
from pydantic import GetCoreSchemaHandler | ||
from pydantic_core.core_schema import CoreSchema | ||
|
||
from pandas._libs.internals import BlockValuesRefs | ||
from pandas._typing import ( | ||
AggFuncType, | ||
|
@@ -6215,3 +6221,15 @@ def cumsum(self, axis: Axis | None = None, skipna: bool = True, *args, **kwargs) | |
@doc(make_doc("cumprod", 1)) | ||
def cumprod(self, axis: Axis | None = None, skipna: bool = True, *args, **kwargs): | ||
return NDFrame.cumprod(self, axis, skipna, *args, **kwargs) | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls, source_type: Type[Any], handler: GetCoreSchemaHandler) -> CoreSchema: | ||
pyd_core = cast("pydantic_core", import_optional_dependency("pydantic_core")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you casting to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You can use ![]() |
||
core_schema = pyd_core.core_schema | ||
|
||
return core_schema.union_schema( | ||
[ | ||
core_schema.is_instance_schema(Series), | ||
core_schema.no_info_plain_validator_function(Series) | ||
] | ||
) | ||
Comment on lines
+6230
to
+6235
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you explain what these do please? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,3 +274,23 @@ def __radd__(self, other): | |
|
||
assert right.__add__(left) is NotImplemented | ||
assert right + left is left | ||
|
||
|
||
@td.skip_if_no("pydantic") | ||
@td.skip_if_no("pydantic_core") | ||
@pytest.mark.parametrize("series", [ | ||
Series([1, 2, 3]), | ||
Series(np.array([1, 2, 3])), | ||
Comment on lines
+282
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these two are the same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I actually should have used |
||
Series({'a': 1, 'b': 2, 'c': 3}), | ||
Series(3), | ||
]) | ||
def test_pydantic_protocol(series: Series) -> None: | ||
from pydantic import BaseModel | ||
|
||
class Model(BaseModel): | ||
series: Series | ||
|
||
|
||
model = Model(series=series) | ||
assert model.model_dump() == {} | ||
assert model.model_json_schema() == {} | ||
Comment on lines
+295
to
+296
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you explain these two lines please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test is not right. I couldn't set up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok I'd suggest following https://pandas.pydata.org/docs/dev/development/contributing_environment.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a few more files where this needs to be added