Skip to content
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

Enhancment/1886 Add json_normalize to pandas read formats #1892

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandera/typing/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Formats(Enum):
#: python pickle file format
pickle = "pickle"

#: python json_normalize
json_normalize = "json_normalize"


Format = Union[
Literal[Formats.csv],
Expand All @@ -50,4 +53,5 @@ class Formats(Enum):
Literal[Formats.feather],
Literal[Formats.parquet],
Literal[Formats.pickle],
Literal[Formats.json_normalize],
]
1 change: 1 addition & 0 deletions pandera/typing/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def from_format(cls, obj: Any, config) -> pd.DataFrame:
Formats.feather: pd.read_feather,
Formats.parquet: pd.read_parquet,
Formats.pickle: pd.read_pickle,
Formats.json_normalize: pd.json_normalize,
}[Formats(config.from_format)]

return reader(obj, **(config.from_format_kwargs or {})) # type: ignore
Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_from_to_format_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Config:
from_format = pd.read_pickle


class InSchemaJsonNormalize(InSchema):
class Config:
from_format = "json_normalize"


class OutSchema(InSchema):
float_col: pa.typing.Series[float]

Expand Down Expand Up @@ -194,6 +199,7 @@ def _needs_pyarrow(schema) -> bool:
[InSchemaParquet, lambda df, x: df.to_parquet(x), io.BytesIO],
[InSchemaPickle, lambda df, x: df.to_pickle(x), io.BytesIO],
[InSchemaPickleCallable, lambda df, x: df.to_pickle(x), io.BytesIO],
[InSchemaJsonNormalize, lambda df: df.to_dict(orient="records"), None],
],
)
def test_from_format(schema, to_fn, buf_cls):
Expand Down
Loading