Skip to content

Avoids exception when pandas.io.json.json_normalize contains items in… #14505

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

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a560331
Avoids exception when pandas.io.json.json_normalize contains items in…
Oct 26, 2016
050bf60
COMPAT/TST: fix test for range testing of negative integers to neg po…
jreback Oct 26, 2016
66b4c83
BLD: Support Cython 0.25
robertwb Oct 26, 2016
6130e77
BUG: Accept unicode quotechars again in pd.read_csv
gfyoung Oct 26, 2016
6ac759d
BLD: fix 3.4 build for cython to 0.24.1
jreback Oct 26, 2016
31ca717
TST: simplify tests for GH14346 (#14502)
jorisvandenbossche Oct 27, 2016
e7ac84d
DOC: Expand on reference docs for read_json() (#14442)
cswarth Oct 27, 2016
d7fb5bd
BUG: fix DatetimeIndex._maybe_cast_slice_bound for empty index (GH143…
jorisvandenbossche Oct 27, 2016
096d886
MAINT: Expand lint for *.py (#14516)
gfyoung Oct 28, 2016
7f5a45c
BUG/ERR: raise correct error when sql driver is not installed (#14527)
jorisvandenbossche Oct 29, 2016
b793443
Added documentation and test for issue #14505
Oct 30, 2016
1ce6299
DOC: Simplify the gbq integration testing procedure for contributors …
parthea Oct 31, 2016
47f117d
BUG: tseries ceil doc fix (#14543)
tworec Oct 31, 2016
b088112
BUG: Don't parse inline quotes in skipped lines (#14514)
gfyoung Oct 31, 2016
60a335e
BUG: Dataframe constructor when given dict with None value (#14392)
brandonmburroughs Oct 31, 2016
e544362
Update ISSUE_TEMPLATE: be more explicit on where to paste the output …
jorisvandenbossche Nov 1, 2016
c3e25c6
Added keyword errors {'raise'|'ignore}
Nov 1, 2016
8b80562
asv compat for py3
jreback Nov 2, 2016
eb7bd99
BUG: don't close user-provided file handles in C parser (GH14418) (#1…
jorisvandenbossche Nov 2, 2016
52f31d4
BUG: DataFrame.quantile with NaNs (GH14357) (#14536)
jorisvandenbossche Nov 2, 2016
1d95179
PERF: casting loc to labels dtype before searchsorted (#14551)
jorisvandenbossche Nov 2, 2016
093aa82
DEPR: add deprecation warning for com.array_equivalent (#14567)
jorisvandenbossche Nov 3, 2016
7f0c4e0
DOC: rst fixes
jorisvandenbossche Nov 3, 2016
252526c
BUG/API: Index.append with mixed object/Categorical indices (#14545)
jorisvandenbossche Nov 3, 2016
e1cdc4b
DOC: update whatsnew/release notes for 0.19.1 (#14573)
jorisvandenbossche Nov 3, 2016
5aaf8fe
Avoids exception when pandas.io.json.json_normalize contains items in…
Oct 26, 2016
8928270
Added documentation and test for issue #14505
Oct 30, 2016
9848837
Added keyword errors {'raise'|'ignore}
Nov 1, 2016
cc2fdc2
Added documentation and test for issue #14505
Oct 30, 2016
33495bc
Merge branch 'master' of https://github.com/dickreuter/pandas
Nov 3, 2016
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/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ Bug Fixes


- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns`` is not scalar and ``values`` is not specified (:issue:`14380`)
- Bug in ``pandas.io.json.json_normalize``When parsing a nested json and convert it to a dataframe, the meta parameter can be used to use fields as metadata for each record in resulting table. In some cases, not all items may contain all of the specified meta fields. This change will avoid throwing an error and output np.nan instead. (:issue '14505')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls simplify. a user just wants to know does this issue pertain to them, and a short expl.

make the issue

(:issue:14505)

5 changes: 4 additions & 1 deletion pandas/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,10 @@ def _recursive_extract(data, path, seen_meta, level=0):
if level + 1 > len(val):
meta_val = seen_meta[key]
else:
meta_val = _pull_field(obj, val[level:])
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a keyword, call it errors='raise'|'ignore'. You are defining ignore. Please leave the default as raise (which is the current behavior).

meta_val = _pull_field(obj, val[level:])
except:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use a bare except, list a specific exception KeyError?

meta_val = np.nan
meta_vals[key].append(meta_val)

records.extend(recs)
Expand Down
45 changes: 45 additions & 0 deletions pandas/io/tests/json/test_json_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,51 @@ def test_nested_flattens(self):

self.assertEqual(result, expected)

def test_json_normalise_fix(self):
j = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment

"Trades": [{
"general": {
"tradeid": 100,
"trade_version": 1,
"stocks": [{

"symbol": "AAPL",
"name": "Apple",
"price": "0"

}, {

"symbol": "GOOG",
"name": "Google",
"price": "0"

}
]
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this prob does not pass linting. make sure it does.

}, {
"general": {
"tradeid": 100,
"stocks": [{

"symbol": "AAPL",
"name": "Apple",
"price": "0"

}, {
"symbol": "GOOG",
"name": "Google",
"price": "0"

}
]
},
}
]
}
j = json_normalize(data=j['Trades'], record_path=[['general', 'stocks']],
meta=[['general', 'tradeid'], ['general', 'trade_version']])
self.assertEqual(len(j), 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

construct the expected frame and use assert_frame_equal


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb',
'--pdb-failure', '-s'], exit=False)