Skip to content

Commit f9e4394

Browse files
BUG: json_normalize prefixed multi-char sep to all keys (#43851)
1 parent 7f7ee38 commit f9e4394

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ I/O
463463
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
464464
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
465465
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43366`)
466+
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
466467
- Bug in :func:`read_csv` with :code:`float_precision="round_trip"` which did not skip initial/trailing whitespace (:issue:`43713`)
467468
-
468469

pandas/io/json/_normalize.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ def _normalise_json(
148148
_normalise_json(
149149
data=value,
150150
# to avoid adding the separator to the start of every key
151+
# GH#43831 avoid adding key if key_string blank
151152
key_string=new_key
152-
if new_key[len(separator) - 1] != separator
153+
if new_key[: len(separator)] != separator
153154
else new_key[len(separator) :],
154155
normalized_dict=normalized_dict,
155156
separator=separator,

pandas/tests/io/json/test_normalize.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ def test_simple_normalize_with_separator(self, deep_nested):
220220
expected = Index(["name", "pop", "country", "states_name"]).sort_values()
221221
assert result.columns.sort_values().equals(expected)
222222

223+
def test_normalize_with_multichar_separator(self):
224+
# GH #43831
225+
data = {"a": [1, 2], "b": {"b_1": 2, "b_2": (3, 4)}}
226+
result = json_normalize(data, sep="__")
227+
expected = DataFrame([[[1, 2], 2, (3, 4)]], columns=["a", "b__b_1", "b__b_2"])
228+
tm.assert_frame_equal(result, expected)
229+
223230
def test_value_array_record_prefix(self):
224231
# GH 21536
225232
result = json_normalize({"A": [1, 2]}, "A", record_prefix="Prefix.")

0 commit comments

Comments
 (0)