Skip to content

Commit aeb36d9

Browse files
wesselhuisingWessel Huising
andauthored
Feature; Make the use of Optional fields possible when parsing df (Optional columns) (#16)
* use the right pyproject.toml to get rid of problems between black and isort * create tests for optional types, adjust code accordanly and create new optional type with helper function to parse nan's * create tests for optional types, adjust code accordanly and create new optional type with helper function to parse nan's * update lock file, update pydantic version and specify deps constraint for future updates * add pandas stubs * add multiprocess to the pyproject.toml * run isort on all files --------- Co-authored-by: Wessel Huising <wesselhuising@gmail.com>
1 parent 03a115c commit aeb36d9

15 files changed

Lines changed: 662 additions & 508 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
language: system
88
types: [python]
99
require_serial: true
10-
args: [--check, --diff , --config, pyproject.toml, src, tests]
10+
args: [--config, pyproject.toml, src, tests]
1111
- id: check-added-large-files
1212
name: check for added large files
1313
entry: check-added-large-files

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
PHONY: black
22

33
black:
4-
black --config pyproject.toml src/
4+
black --config pyproject.toml .

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,24 @@ df_raised_error = DataFrameSchema.parse_df(
8585
errors="raise",
8686
)
8787
```
88+
89+
## Special fields and types
90+
### Optional
91+
As the DataFrame is being parsed into a dict, a `None` value is considered as a `nan` value in cases there are different values in the dict. Therefore, specifying `Optional` columns (where the value can be empty) can be speciyfied by using the custom `pandantic.Optional` type. This type is a replacement for `typing.Optional`.
92+
93+
```
94+
from pandantic import BaseModel, Optional
95+
96+
# GIVEN
97+
class Model(BaseModel):
98+
a: Optional[int] = None
99+
b: int
100+
101+
df_example = pd.DataFrame({"a": [1, None, 2], "b": ["str", 2, 3]})
102+
103+
# WHEN
104+
df_filtered = Model.parse_df(df_example, errors="filter", verbose=True)
105+
```
106+
88107
## Docs
89108
Documentation can be found [here](https://pandantic-rtd.readthedocs.io/en/latest/)

0 commit comments

Comments
 (0)