Skip to content

BUG: make JSONTableWriter fail if no index.name and 'index' in columns #58985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ MultiIndex
I/O
^^^
- Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`)
- Bug in :meth:`.DataFrame.to_json` when ``"index"`` was a value in the :attr:`DataFrame.column` and :attr:`Index.name` was ``None``. Now, this will fail with a ``ValueError`` (:issue:`58925`)
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
- Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2387,7 +2387,8 @@ def to_json(
index : bool or None, default None
The index is only used when 'orient' is 'split', 'index', 'column',
or 'table'. Of these, 'index' and 'column' do not support
`index=False`.
`index=False`. The string 'index' as a column name with empty :class:`Index`
or if it is 'index' will raise a ``ValueError``.

indent : int, optional
Length of whitespace used to indent each record.
Expand Down
3 changes: 3 additions & 0 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from pandas.io.json._table_schema import (
build_table_schema,
parse_table_schema,
set_default_names,
)
from pandas.io.parsers.readers import validate_integer

Expand Down Expand Up @@ -353,6 +354,8 @@ def __init__(
raise ValueError(msg)

self.schema = build_table_schema(obj, index=self.index)
if self.index:
obj = set_default_names(obj)

# NotImplemented on a column MultiIndex
if obj.ndim == 2 and isinstance(obj.columns, MultiIndex):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,13 @@ def test_to_json_from_json_columns_dtypes(self, orient):
)
tm.assert_frame_equal(result, expected)

def test_to_json_with_index_as_a_column_name(self):
df = DataFrame(data={"index": [1, 2], "a": [2, 3]})
with pytest.raises(
ValueError, match="Overlapping names between the index and columns"
):
df.to_json(orient="table")

@pytest.mark.parametrize("dtype", [True, {"b": int, "c": int}])
def test_read_json_table_dtype_raises(self, dtype):
# GH21345
Expand Down