-
Notifications
You must be signed in to change notification settings - Fork 70
✨ add pandas.Series
#68
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
Open
EkberHasanov
wants to merge
34
commits into
pydantic:main
Choose a base branch
from
EkberHasanov:type/pandas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
89a2c38
Add support and test for pd.Series
EkberHasanov b726b19
Adding docs for basic usage
EkberHasanov 24bf87a
Fix documentation
EkberHasanov 1bb8a71
Add pandas to optional dependencies
EkberHasanov bc37f7f
fix python3.8 issues
EkberHasanov 43be7f6
fix py3.7 issues
EkberHasanov 50b0547
fix macos error
EkberHasanov 16aa656
fix indentation error in ci.yml
EkberHasanov 0d899c9
ci: fix dependency issue in macos
EkberHasanov 33f45f2
improve test coverage to 100%
EkberHasanov 98e58d6
🔧 update code
yezz123 b4a35ac
🍱 update requirements
yezz123 1660ca7
Merge branch 'main' into type/pandas
yezz123 b1e04f0
🍱 Fix Requirements
yezz123 2ac57cd
🍱 Fix Requirements
yezz123 339c71b
.
yezz123 7129b91
fix
yezz123 c9c4f12
Update pydantic_extra_types/pandas_types.py
yezz123 cc9dac0
Inheriting directly from pd.Series
EkberHasanov 501c173
Merge branch 'main' into type/pandas
EkberHasanov 09e073d
delete extra files
EkberHasanov b15b3d0
upgrading version of pandas
EkberHasanov 2718471
Merge branch 'main' into type/pandas
EkberHasanov 8e408bc
Update pyproject.txt
EkberHasanov 7b4433f
Update test_json_schema.py
EkberHasanov 6cff2d7
Update linting.in
EkberHasanov 3d7b805
adding pandas-stubs
EkberHasanov b751633
fix versions
EkberHasanov d78fc5a
resolve version issue
EkberHasanov 6a55cd2
upgrading version of numpy
EkberHasanov ba7a941
fixing some issues
EkberHasanov 933540a
change core_schema func
EkberHasanov 25da211
Merge branch 'main' into type/pandas
EkberHasanov 811d664
Update test_json_schema.py
EkberHasanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
The `Series` class provides support for working with pandas Series objects. | ||
|
||
```py | ||
import pandas as pd | ||
from pydantic import BaseModel | ||
|
||
from pydantic_extra_types.pandas_types import Series | ||
|
||
|
||
class MyData(BaseModel): | ||
numbers: Series | ||
|
||
|
||
data = {"numbers": pd.Series([1, 2, 3, 4, 5])} | ||
my_data = MyData(**data) | ||
|
||
print(my_data.numbers) | ||
# > 0 1 | ||
# > 1 2 | ||
# > 2 3 | ||
# > 3 4 | ||
# > 4 5 | ||
# > dtype: int64 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import pandas as pd | ||
from pydantic import GetCoreSchemaHandler | ||
from pydantic_core import core_schema | ||
|
||
|
||
class Series(pd.Series): # type: ignore | ||
@classmethod | ||
def __get_pydantic_core_schema__( | ||
cls, source: type[Any], handler: GetCoreSchemaHandler | ||
) -> core_schema.BeforeValidatorFunctionSchema: | ||
return core_schema.general_before_validator_function( | ||
cls._validate, | ||
core_schema.any_schema(), | ||
) | ||
|
||
@classmethod | ||
def _validate(cls, __input_value: Any, _: core_schema.ValidationInfo) -> Series: | ||
return cls(__input_value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pre-commit | |
mypy | ||
annotated-types | ||
ruff | ||
pandas-stubs | ||
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. Likely not needed on pandas >= 2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import pandas as pd | ||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from pydantic_extra_types.pandas_types import Series | ||
|
||
|
||
@pytest.fixture(scope='session', name='SeriesModel') | ||
def series_model_fixture(): | ||
class SeriesModel(BaseModel): | ||
data: Series | ||
|
||
return SeriesModel | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'data, expected', | ||
[ | ||
([1, 2, 3], [1, 2, 3]), | ||
([], []), | ||
([10, 20, 30, 40], [10, 20, 30, 40]), | ||
], | ||
) | ||
def test_series_creation(data, expected): | ||
if pd.__version__ <= '1.5.3' and data == []: | ||
s = Series([1]) | ||
expected = [1] | ||
else: | ||
s = Series(data) | ||
assert isinstance(s, Series) | ||
assert isinstance(s, pd.Series) | ||
assert s.tolist() == expected | ||
|
||
|
||
def test_series_repr(): | ||
data = [1, 2, 3] | ||
s = Series(data) | ||
assert repr(s) == repr(pd.Series(data)) | ||
|
||
|
||
def test_series_attribute_access(): | ||
data = [1, 2, 3] | ||
s = Series(data) | ||
assert s.sum() == pd.Series(data).sum() | ||
|
||
|
||
def test_series_equality(): | ||
data = [1, 2, 3] | ||
s1 = Series(data) | ||
s2 = Series(data) | ||
assert s1.equals(other=s2) | ||
assert s2.equals(pd.Series(data)) | ||
|
||
|
||
def test_series_addition(): | ||
data1 = [1, 2, 3] | ||
data2 = [4, 5, 6] | ||
s1 = Series(data1) | ||
s2 = Series(data2) | ||
s3 = s1 + s2 | ||
assert isinstance(s3, pd.Series) | ||
assert s3.tolist() == [5, 7, 9] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'data, other, expected', | ||
[ | ||
([1, 2, 3], [4, 5, 6], [5, 7, 9]), | ||
([10, 20, 30], (1, 2, 3), [11, 22, 33]), | ||
([5, 10, 15], pd.Series([1, 2, 3]), [6, 12, 18]), | ||
], | ||
) | ||
def test_series_addition_with_types(data, other, expected): | ||
s = Series(data) | ||
result = s + other | ||
assert isinstance(result, pd.Series) | ||
assert result.tolist() == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'data, other', | ||
[ | ||
([1, 2, 3], 'invalid'), # Invalid type for addition | ||
([1, 2, 3], {'a': 1, 'b': 2}), # Invalid type for addition | ||
], | ||
) | ||
def test_series_addition_invalid_type_error(data, other) -> None: | ||
s = Series(data) | ||
with pytest.raises(TypeError): | ||
s + other | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'data, other', | ||
[ | ||
([1, 2, 3], []), | ||
], | ||
) | ||
def test_series_addition_invalid_value_error(data, other) -> None: | ||
s = Series(data) | ||
with pytest.raises(ValueError): | ||
s + other | ||
|
||
|
||
def test_valid_series_model(SeriesModel) -> None: | ||
model = SeriesModel(data=[1, 2, 4]) | ||
assert isinstance(model.data, pd.Series) | ||
assert model.data.equals(pd.Series([1, 2, 4])) | ||
|
||
|
||
def test_valid_series_model_with_pd_series(SeriesModel) -> None: | ||
s = pd.Series([1, 2, 4]) | ||
model = SeriesModel(data=s) | ||
assert isinstance(model.data, pd.Series) | ||
assert model.data.equals(s) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think this is the best we can do. Is it? A series should be an
array
of some type, and I guess we are able to get this type as well.