From 983f58e10862895f54ba4f3cea78ebf252ea19c6 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 2 May 2017 14:47:26 -0500 Subject: [PATCH] BUG: Fixed renaming of falsey names in build_table_schema Closes https://github.com/pandas-dev/pandas/issues/16203 --- pandas/io/json/table_schema.py | 6 +++++- pandas/tests/io/json/test_json_table_schema.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pandas/io/json/table_schema.py b/pandas/io/json/table_schema.py index d8ef3afc9591f..c3865afa9c0c0 100644 --- a/pandas/io/json/table_schema.py +++ b/pandas/io/json/table_schema.py @@ -76,7 +76,11 @@ def set_default_names(data): def make_field(arr, dtype=None): dtype = dtype or arr.dtype - field = {'name': arr.name or 'values', + if arr.name is None: + name = 'values' + else: + name = arr.name + field = {'name': name, 'type': as_json_table_type(dtype)} if is_categorical_dtype(arr): diff --git a/pandas/tests/io/json/test_json_table_schema.py b/pandas/tests/io/json/test_json_table_schema.py index 0f77a886dd302..c3a976973bb29 100644 --- a/pandas/tests/io/json/test_json_table_schema.py +++ b/pandas/tests/io/json/test_json_table_schema.py @@ -461,3 +461,11 @@ def test_overlapping_names(self): data.to_json(orient='table') assert 'Overlapping' in str(excinfo.value) + + def test_mi_falsey_name(self): + # GH 16203 + df = pd.DataFrame(np.random.randn(4, 4), + index=pd.MultiIndex.from_product([('A', 'B'), + ('a', 'b')])) + result = [x['name'] for x in build_table_schema(df)['fields']] + assert result == ['level_0', 'level_1', 0, 1, 2, 3]