|
1 | 1 | import pytest |
2 | 2 | import numpy as np |
3 | 3 | import json |
| 4 | +import math |
4 | 5 |
|
5 | 6 | import pandas.util.testing as tm |
6 | 7 | from pandas import compat, Index, DataFrame |
@@ -54,6 +55,17 @@ def state_data(): |
54 | 55 | 'state': 'Ohio'}] |
55 | 56 |
|
56 | 57 |
|
| 58 | +@pytest.fixture |
| 59 | +def author_missing_data(): |
| 60 | + return [ |
| 61 | + {'info': None}, |
| 62 | + {'info': |
| 63 | + {'created_at': '11/08/1993', 'last_updated': '26/05/2012'}, |
| 64 | + 'author_name': |
| 65 | + {'first': 'Jane', 'last_name': 'Doe'} |
| 66 | + }] |
| 67 | + |
| 68 | + |
57 | 69 | class TestJSONNormalize(object): |
58 | 70 |
|
59 | 71 | def test_simple_records(self): |
@@ -226,6 +238,21 @@ def test_non_ascii_key(self): |
226 | 238 | result = json_normalize(json.loads(testjson)) |
227 | 239 | tm.assert_frame_equal(result, expected) |
228 | 240 |
|
| 241 | + def test_missing_field(self, author_missing_data): |
| 242 | + result = json_normalize(author_missing_data) |
| 243 | + ex_data = [ |
| 244 | + {'author_name.first': math.nan, |
| 245 | + 'author_name.last_name': math.nan, |
| 246 | + 'info.created_at': math.nan, |
| 247 | + 'info.last_updated': math.nan}, |
| 248 | + {'author_name.first': 'Jane', |
| 249 | + 'author_name.last_name': 'Doe', |
| 250 | + 'info.created_at': '11/08/1993', |
| 251 | + 'info.last_updated': '26/05/2012'} |
| 252 | + ] |
| 253 | + expected = DataFrame(ex_data) |
| 254 | + tm.assert_frame_equal(result, expected) |
| 255 | + |
229 | 256 |
|
230 | 257 | class TestNestedToRecord(object): |
231 | 258 |
|
@@ -322,3 +349,26 @@ def test_json_normalize_errors(self): |
322 | 349 | ['general', 'trade_version']], |
323 | 350 | errors='raise' |
324 | 351 | ) |
| 352 | + |
| 353 | + def test_nonetype_dropping(self): |
| 354 | + data = [ |
| 355 | + {'info': None, |
| 356 | + 'author_name': |
| 357 | + {'first': 'Smith', 'last_name': 'Appleseed'} |
| 358 | + }, |
| 359 | + {'info': |
| 360 | + {'created_at': '11/08/1993', 'last_updated': '26/05/2012'}, |
| 361 | + 'author_name': |
| 362 | + {'first': 'Jane', 'last_name': 'Doe'} |
| 363 | + } |
| 364 | + ] |
| 365 | + result = nested_to_record(data) |
| 366 | + expected = [ |
| 367 | + {'author_name.first': 'Smith', |
| 368 | + 'author_name.last_name': 'Appleseed'}, |
| 369 | + {'author_name.first': 'Jane', |
| 370 | + 'author_name.last_name': 'Doe', |
| 371 | + 'info.created_at': '11/08/1993', |
| 372 | + 'info.last_updated': '26/05/2012'}] |
| 373 | + |
| 374 | + assert result == expected |
0 commit comments