Skip to content

Commit

Permalink
Enhancment/1886 Add json_normalize to pandas read formats (unionai-os…
Browse files Browse the repository at this point in the history
…s#1892)

* add json_normalize to pandas read formats

* add unit test

* format code with black/fix unit test
  • Loading branch information
Jarek-Rolski authored and max-raphael committed Jan 24, 2025
1 parent 3b638df commit aca3850
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
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

0 comments on commit aca3850

Please sign in to comment.