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

ENH: Support pd.json_normalize for normalizing only meta fields #60460

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Other enhancements
- Support passing a :class:`Iterable[Hashable]` input to :meth:`DataFrame.drop_duplicates` (:issue:`59237`)
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)
- :func:`pandas.json_normalize` now supports normalizing specified ``meta`` fields from an array of records when the ``record_path`` is ``None`` or an empty list (``[]``)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.notable_bug_fixes:
Expand Down
33 changes: 21 additions & 12 deletions pandas/io/json/_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
):
return DataFrame(_simple_json_normalize(data, sep=sep), index=index)

if record_path is None:
if record_path is None and meta is None:
if any([isinstance(x, dict) for x in y.values()] for y in data):
# naive normalization, this is idempotent for flat records
# and potentially will inflate the data considerably for
Expand All @@ -525,6 +525,8 @@ def _pull_records(js: dict[str, Any], spec: list | str) -> list:
# reasonably
data = nested_to_record(data, sep=sep, max_level=max_level)
return DataFrame(data, index=index)
elif record_path is None and meta is not None:
record_path = []
elif not isinstance(record_path, list):
record_path = [record_path]

Expand Down Expand Up @@ -554,23 +556,30 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
_recursive_extract(obj[path[0]], path[1:], seen_meta, level=level + 1)
else:
for obj in data:
recs = _pull_records(obj, path[0])
recs = [
nested_to_record(r, sep=sep, max_level=max_level)
if isinstance(r, dict)
else r
for r in recs
]

# For repeating the metadata later
lengths.append(len(recs))
if len(path) == 1:
recs = _pull_records(obj, path[0])
recs = [
nested_to_record(r, sep=sep, max_level=max_level)
if isinstance(r, dict)
else r
for r in recs
]
records.extend(recs)

# For repeating the metadata later
lengths.append(len(recs))
else:
# If path is an empty list, data is treated as an
# array of records, and only the meta fields will
# be extract from each record.
lengths.append(1)

for val, key in zip(_meta, meta_keys):
if level + 1 > len(val):
meta_val = seen_meta[key]
else:
meta_val = _pull_field(obj, val[level:])
meta_vals[key].append(meta_val)
records.extend(recs)

_recursive_extract(data, record_path, {}, level=0)

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,29 @@ def test_series_index(self, state_data):
result = json_normalize(series, "counties")
tm.assert_index_equal(result.index, idx.repeat([3, 2]))

@pytest.mark.parametrize("path", [None, []])
def test_empty_record_path_and_not_empty_meta(self, state_data, path):
ex_data = [
{
"shortname": "FL",
"state": "Florida",
"info.governor": "Rick Scott",
},
{
"shortname": "OH",
"state": "Ohio",
"info.governor": "John Kasich",
},
]
expected = DataFrame(ex_data)

result = json_normalize(
state_data,
record_path=path,
meta=["shortname", "state", ["info", "governor"]],
)
tm.assert_frame_equal(result, expected)


class TestNestedToRecord:
def test_flat_stays_flat(self):
Expand Down
Loading