Skip to content

BUG: OverflowError on DataFrame.dtypes.to_json#66361

Open
natmokval wants to merge 7 commits into
pandas-dev:mainfrom
natmokval:BUG-df.dtypes.to_json-OverflowError
Open

BUG: OverflowError on DataFrame.dtypes.to_json#66361
natmokval wants to merge 7 commits into
pandas-dev:mainfrom
natmokval:BUG-df.dtypes.to_json-OverflowError

Conversation

@natmokval

@natmokval natmokval commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

The example below previously raised OverflowError: Maximum recursion level reached

import pandas as pd

ser = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}).dtypes
ser.to_json()

I used AI to help me with this pull request, specifically GPT-5.6 Thinking

@natmokval natmokval added Bug IO JSON read_json, to_json, json_normalize labels Jul 18, 2026
Comment thread pandas/io/json/_json.py Outdated
if (
default_handler is None
and isinstance(obj_to_write, ABCSeries)
and any(isinstance(obj, np.dtype) for obj in obj_to_write.array)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think we want to iterate in python like this. If nothing else, probably check dtype=object first

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @jbrockmendel for the comment. I added the check dtype=object. Maybe we could use infer_dtype. What do you think? I am not sure about it because infer_dtype returns "unknown-array" in this case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the right place to do this is in objToJSON.c. Use PyArray_DescrCheck in Object_beginTypeContext. Also pls write tests for all the relevant cases, off the top of my head:

dts = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}).dtypes
dts.to_json(orient="split", index=False)
dts.to_json(orient="table")
dts.to_frame().to_json()

Comment thread doc/source/whatsnew/v3.1.0.rst Outdated
- Bug in :meth:`DataFrame.to_json` and :meth:`Series.to_json` with ``orient="table"`` silently dropping the timezone of columns with a fixed-offset timezone (e.g. ``datetime.timezone(timedelta(hours=1))``), so the offset was lost on round-trip through :func:`read_json` (:issue:`39537`)
- Bug in :meth:`DataFrame.to_stata` raising ``KeyError`` when column names require renaming and ``convert_dates`` is specified for a different column (:issue:`60536`)
- Bug in :meth:`DataFrame.to_string` where ``formatters`` dict was applied to wrong columns when output was horizontally truncated via ``max_cols`` (:issue:`35410`)
- Bug in :meth:`Series.to_json` and :meth:`DataFrame.to_json` which raised an OverflowError when serializing ``DataFrame.dtypes`` or ``DataFrame.dtypes.to_frame`` (:issue:`61170`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is just about np.dtype or ExtensionDtype objects?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i suspect this doesnt handle ExtensionDtypes well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is just about np.dtype or ExtensionDtype objects?

Yes, it covers only np.dtype. I will update the whatsnew note.

@natmokval natmokval Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i suspect this doesnt handle ExtensionDtypes well?

In a way, it also handles ExtensionDtype objects. On main, the example below raises:
ValueError: Unable to serialize object to JSON: encountered an unsupported object type; convert the column to a supported type (e.g. str) before calling to_json.

ser = DataFrame(
      {
           "A": Series([1, 2, 3], dtype="Int64"),
           "B": Series([4, 5, 6], dtype="Int64"),   
        }
).dtypes
result = ser.to_json()
    

With the current changes, it passes. I think we expect this example to raise an error. I am not sure how to handle this.

@jbrockmendel

Copy link
Copy Markdown
Member

BTW if you're using AI to help write PRs please acknowledge it in the OP, ideally with a description of which one (more specific is better)

@natmokval

Copy link
Copy Markdown
Contributor Author

BTW if you're using AI to help write PRs please acknowledge it in the OP, ideally with a description of which one (more specific is better)

Yes, sure. I added the name of the AI tool I used to the PR description. I hope using such tools is allowed.

@jbrockmendel

Copy link
Copy Markdown
Member

Yes, sure. I added the name of the AI tool I used to the PR description. I hope using such tools is allowed.

Absolutely. In the PR using AI is fine. Its only in the comments we ask for exclusively human interaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug IO JSON read_json, to_json, json_normalize

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.dtypes.to_json OverflowError: Maximum recursion level reached

2 participants