Skip to content

Commit d298588

Browse files
author
dickreuter
committed
Fixed as instructed in pull request page
1 parent bcfbf18 commit d298588

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

doc/source/whatsnew/v0.20.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Other enhancements
6161
- The ``usecols`` argument in ``pd.read_csv`` now accepts a callable function as a value (:issue:`14154`)
6262
- ``pd.DataFrame.plot`` now prints a title above each subplot if ``suplots=True`` and ``title`` is a list of strings (:issue:`14753`)
6363
- ``pd.Series.interpolate`` now supports timedelta as an index type with ``method='time'`` (:issue:`6424`)
64+
- ``pandas.io.json.json_normalize`` If meta keys are not always present a new option to set errors="ignore" (:issue:`14583`)
65+
6466

6567
.. _whatsnew_0200.api_breaking:
6668

pandas/io/json.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,9 @@ def _recursive_extract(data, path, seen_meta, level=0):
849849
if errors == 'ignore':
850850
meta_val = np.nan
851851
else:
852-
raise KeyError(
853-
"Try running with errors='ignore' as the following key may not always be present: " + str(e))
852+
raise \
853+
KeyError(
854+
"Try running with errors='ignore' as key may not always be present: %s", e)
854855
meta_vals[key].append(meta_val)
855856

856857
records.extend(recs)

pandas/io/tests/json/test_json_norm.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def test_nested_flattens(self):
226226
self.assertEqual(result, expected)
227227

228228

229-
def test_json_normalise_fix(self):
230-
# issue 14505
231-
j = {
229+
def test_json_normalize_errors(self):
230+
# If meta keys are not always present a new option to set errors='ignore' has been implemented (:issue:`14583`)
231+
i = {
232232
"Trades": [{
233233
"general": {
234234
"tradeid": 100,
@@ -268,7 +268,7 @@ def test_json_normalise_fix(self):
268268
}
269269
]
270270
}
271-
j = json_normalize(data=j['Trades'], record_path=[['general', 'stocks']],
271+
j = json_normalize(data=i['Trades'], record_path=[['general', 'stocks']],
272272
meta=[['general', 'tradeid'], ['general', 'trade_version']], errors='ignore')
273273
expected={'general.trade_version': {0: 1.0, 1: 1.0, 2: '', 3: ''},
274274
'general.tradeid': {0: 100, 1: 100, 2: 100, 3: 100},
@@ -278,6 +278,12 @@ def test_json_normalise_fix(self):
278278

279279
self.assertEqual(j.fillna('').to_dict(), expected)
280280

281+
self.assertRaises(KeyError,
282+
json_normalize, data=i['Trades'], record_path=[['general', 'stocks']],
283+
meta=[['general', 'tradeid'], ['general', 'trade_version']], errors='raise'
284+
)
285+
286+
281287
if __name__ == '__main__':
282288
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb',
283289
'--pdb-failure', '-s'], exit=False)

0 commit comments

Comments
 (0)