Skip to content

Commit 7e5515b

Browse files
felipemaionmroeschke
authored and
im-vinicius
committed
* Fix BUG: pandas-dev#37782 * Fix BUG: pandas-dev#37782 * Fix BUG: 37782 - Hardcoded test data * Fix BUG: 37782 - Hardcoded test data * Update pandas/io/json/_normalize.py Co-authored-by: Matthew Roeschke <[email protected]> * Fix BUG: 37782 - typo * Fix BUG: 37782 - typo * Update doc/source/whatsnew/v2.1.0.rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 8b4ba39 commit 7e5515b

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ I/O
378378
^^^
379379
- :meth:`DataFrame.to_orc` now raising ``ValueError`` when non-default :class:`Index` is given (:issue:`51828`)
380380
- :meth:`DataFrame.to_sql` now raising ``ValueError`` when the name param is left empty while using SQLAlchemy to connect (:issue:`52675`)
381+
- Bug in :func:`json_normalize`, fix json_normalize cannot parse metadata fields list type (:issue:`37782`)
381382
- Bug in :func:`read_hdf` not properly closing store after a ``IndexError`` is raised (:issue:`52781`)
382383
- Bug in :func:`read_html`, style elements were read into DataFrames (:issue:`52197`)
383384
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)

pandas/io/json/_normalize.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -535,5 +535,15 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
535535
raise ValueError(
536536
f"Conflicting metadata name {k}, need distinguishing prefix "
537537
)
538-
result[k] = np.array(v, dtype=object).repeat(lengths)
538+
# GH 37782
539+
540+
values = np.array(v, dtype=object)
541+
542+
if values.ndim > 1:
543+
# GH 37782
544+
values = np.empty((len(v),), dtype=object)
545+
for i, v in enumerate(v):
546+
values[i] = v
547+
548+
result[k] = values.repeat(lengths)
539549
return result

pandas/tests/io/json/test_normalize.py

+14
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,20 @@ def test_simple_normalize(self, state_data):
170170

171171
tm.assert_frame_equal(result, expected)
172172

173+
def test_fields_list_type_normalize(self):
174+
parse_metadata_fields_list_type = [
175+
{"values": [1, 2, 3], "metadata": {"listdata": [1, 2]}}
176+
]
177+
result = json_normalize(
178+
parse_metadata_fields_list_type,
179+
record_path=["values"],
180+
meta=[["metadata", "listdata"]],
181+
)
182+
expected = DataFrame(
183+
{0: [1, 2, 3], "metadata.listdata": [[1, 2], [1, 2], [1, 2]]}
184+
)
185+
tm.assert_frame_equal(result, expected)
186+
173187
def test_empty_array(self):
174188
result = json_normalize([])
175189
expected = DataFrame()

0 commit comments

Comments
 (0)