Skip to content

Commit 2c903d5

Browse files
davidfischer-chjreback
authored andcommitted
json_normalize: Make code more pythonic and avoid modification of meta if mutable (#18610)
1 parent e99cb9c commit 2c903d5

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ I/O
9090
- Bug in parsing integer datetime-like columns with specified format in ``read_sql`` (:issue:`17855`).
9191
- Bug in :meth:`DataFrame.to_msgpack` when serializing data of the numpy.bool_ datatype (:issue:`18390`)
9292
- Bug in :func:`read_json` not decoding when reading line deliminted JSON from S3 (:issue:`17200`)
93+
- Bug in :func:`pandas.io.json.json_normalize` to avoid modification of ``meta`` (:issue:`18610`)
9394

9495
Plotting
9596
^^^^^^^^

pandas/io/json/normalize.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _pull_field(js, spec):
181181

182182
return result
183183

184-
if isinstance(data, list) and len(data) is 0:
184+
if isinstance(data, list) and not data:
185185
return DataFrame()
186186

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

210-
for i, x in enumerate(meta):
211-
if not isinstance(x, list):
212-
meta[i] = [x]
210+
meta = [m if isinstance(m, list) else [m] for m in meta]
213211

214212
# Disastrously inefficient for now
215213
records = []

pandas/tests/io/json/test_normalize.py

+15
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,21 @@ def test_meta_name_conflict(self):
173173
for val in ['metafoo', 'metabar', 'foo', 'bar']:
174174
assert val in result
175175

176+
def test_meta_parameter_not_modified(self):
177+
# GH 18610
178+
data = [{'foo': 'hello',
179+
'bar': 'there',
180+
'data': [{'foo': 'something', 'bar': 'else'},
181+
{'foo': 'something2', 'bar': 'else2'}]}]
182+
183+
COLUMNS = ['foo', 'bar']
184+
result = json_normalize(data, 'data', meta=COLUMNS,
185+
meta_prefix='meta')
186+
187+
assert COLUMNS == ['foo', 'bar']
188+
for val in ['metafoo', 'metabar', 'foo', 'bar']:
189+
assert val in result
190+
176191
def test_record_prefix(self, state_data):
177192
result = json_normalize(state_data[0], 'counties')
178193
expected = DataFrame(state_data[0]['counties'])

0 commit comments

Comments
 (0)