Skip to content

Commit cbea941

Browse files
committed
adding unit tests (pandas-dev#41876)
1 parent 1575f2f commit cbea941

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

pandas/tests/io/json/test_normalize.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def missing_metadata():
105105
"zip": 44646,
106106
}
107107
],
108+
"previous_residences": {"cities": [{"city_name": "Foo York City"}]},
108109
},
109110
{
110111
"addresses": [
@@ -115,7 +116,8 @@ def missing_metadata():
115116
"state": "TN",
116117
"zip": 37643,
117118
}
118-
]
119+
],
120+
"previous_residences": {"cities": [{"city_name": "Barmingham"}]},
119121
},
120122
]
121123

@@ -623,6 +625,38 @@ def test_missing_meta(self, missing_metadata):
623625
expected = DataFrame(ex_data, columns=columns)
624626
tm.assert_frame_equal(result, expected)
625627

628+
def test_missing_meta_multilevel_record_path_errors_raise(self, missing_metadata):
629+
# GH41876
630+
# Ensure errors='raise' works as intended even when a record_path of length
631+
# greater than one is passed in
632+
msg = "Try running with errors='ignore' as key 'name' is not always present"
633+
with pytest.raises(KeyError, match=msg):
634+
json_normalize(
635+
data=missing_metadata,
636+
record_path=["previous_residences", "cities"],
637+
meta="name",
638+
errors="raise",
639+
)
640+
641+
def test_missing_meta_multilevel_record_path_errors_ignore(self, missing_metadata):
642+
# GH41876
643+
# Ensure errors='ignore' works as intended even when a record_path of length
644+
# greater than one is passed in
645+
result = json_normalize(
646+
data=missing_metadata,
647+
record_path=["previous_residences", "cities"],
648+
meta="name",
649+
errors="ignore",
650+
)
651+
ex_data = [
652+
["Foo York City", "Alice"],
653+
["Barmingham", np.nan],
654+
]
655+
columns = ["city_name", "name"]
656+
columns = ["city_name", "name"]
657+
expected = DataFrame(ex_data, columns=columns)
658+
tm.assert_frame_equal(result, expected)
659+
626660
def test_donot_drop_nonevalues(self):
627661
# GH21356
628662
data = [

0 commit comments

Comments
 (0)