Skip to content

Commit

Permalink
json_normalize: Make code more pythonic and avoid modification of met…
Browse files Browse the repository at this point in the history
…a if mutable (#18610)

(cherry picked from commit 2c903d5)
  • Loading branch information
davidfischer-ch authored and TomAugspurger committed Dec 11, 2017
1 parent 3566c56 commit bbd205e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ I/O
- Bug in parsing integer datetime-like columns with specified format in ``read_sql`` (:issue:`17855`).
- Bug in :meth:`DataFrame.to_msgpack` when serializing data of the numpy.bool_ datatype (:issue:`18390`)
- Bug in :func:`read_json` not decoding when reading line deliminted JSON from S3 (:issue:`17200`)
- Bug in :func:`pandas.io.json.json_normalize` to avoid modification of ``meta`` (:issue:`18610`)


Plotting
Expand Down
6 changes: 2 additions & 4 deletions pandas/io/json/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def _pull_field(js, spec):

return result

if isinstance(data, list) and len(data) is 0:
if isinstance(data, list) and not data:
return DataFrame()

# A bit of a hackjob
Expand All @@ -207,9 +207,7 @@ def _pull_field(js, spec):
elif not isinstance(meta, list):
meta = [meta]

for i, x in enumerate(meta):
if not isinstance(x, list):
meta[i] = [x]
meta = [m if isinstance(m, list) else [m] for m in meta]

# Disastrously inefficient for now
records = []
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/json/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ def test_meta_name_conflict(self):
for val in ['metafoo', 'metabar', 'foo', 'bar']:
assert val in result

def test_meta_parameter_not_modified(self):
# GH 18610
data = [{'foo': 'hello',
'bar': 'there',
'data': [{'foo': 'something', 'bar': 'else'},
{'foo': 'something2', 'bar': 'else2'}]}]

COLUMNS = ['foo', 'bar']
result = json_normalize(data, 'data', meta=COLUMNS,
meta_prefix='meta')

assert COLUMNS == ['foo', 'bar']
for val in ['metafoo', 'metabar', 'foo', 'bar']:
assert val in result

def test_record_prefix(self, state_data):
result = json_normalize(state_data[0], 'counties')
expected = DataFrame(state_data[0]['counties'])
Expand Down

0 comments on commit bbd205e

Please sign in to comment.