Skip to content
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

DOC: Add example with numpy_nullable to pd.read_json() #56473

Closed
wants to merge 17 commits into from

Conversation

linus-md
Copy link
Contributor

@linus-md linus-md commented Dec 12, 2023

@linus-md linus-md changed the title DOC: Add example with numpy_nullable to pd.read_json() DOC: Add example with numpy_nullable to pd.read_json() Dec 12, 2023
@linus-md
Copy link
Contributor Author

Is this related to my changes?

Traceback (most recent call last):
  File "/home/runner/work/pandas/pandas/doc/make.py", line 392, in <module>
    sys.exit(main())
  File "/home/runner/work/pandas/pandas/doc/make.py", line 373, in main
    globals()["pandas"] = importlib.import_module("pandas")
  File "/home/runner/micromamba/envs/test/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/runner/work/pandas/pandas/pandas/__init__.py", line 138, in <module>
    from pandas import api, arrays, errors, io, plotting, tseries
  File "/home/runner/work/pandas/pandas/pandas/api/__init__.py", line 2, in <module>
    from pandas.api import (
  File "/home/runner/work/pandas/pandas/pandas/api/typing/__init__.py", line 31, in <module>
    from pandas.io.json._json import JsonReader
  File "/home/runner/work/pandas/pandas/pandas/io/json/__init__.py", line 1, in <module>
    from pandas.io.json._json import (
  File "/home/runner/work/pandas/pandas/pandas/io/json/_json.py", line 508, in <module>
    def read_json(
  File "/home/runner/work/pandas/pandas/pandas/util/_decorators.py", line 379, in decorator
    params_applied = [
  File "/home/runner/work/pandas/pandas/pandas/util/_decorators.py", line 380, in <listcomp>
    component.format(**params)
KeyError: '"index"'

@linus-md
Copy link
Contributor Author

@mroeschke can you help me here?
When I do this the do this I get no error when I import pandas from pandas-dev env.

>>> data = '{{"index":{{"0":0,"1":1}},"a":{{"0":1,"1":null}},' \
    ...        '"b":{{"0":2.5,"1":4.5}},"c":{{"0":true,"1":false}},' \
    ...        '"d":{{"0":"a","1":"b"}},"e":{{"0":1577.2,"1":1577.1}}}}'
    >>> df = pd.read_json(data, dtype_backend="numpy_nullable")
    >>> print(df)
       index     a    b      c  d       e
    0      0     1  2.5   True  a  1577.2
    1      1  <NA>  4.5  False  b  1577.1

But I get: ValueError: Key name of object must be 'string' when decoding 'object'. When the example is

>>> data = '{"index": {"0": 0, "1": 1}, ' \
    ...        '"a": {"0": 1, "1": null}, ' \
    ...        '"b": {"0": 2.5, "1": 4.5}, ' \
    ...        '"c": {"0": true, "1": false}, ' \
    ...        '"d": {"0": "a", "1": "b"}, ' \
    ...        '"e": {"0": 1577.2, "1": 1577.1}}'
    >>> df = pd.read_json(data, dtype_backend="numpy_nullable")
    >>> print(df)
       index     a    b      c  d       e
    0      0     1  2.5   True  a  1577.2
    1      1  <NA>  4.5  False  b  1577.1

This happens:

>>> import pandas as pd
+ /Users/linussommer/miniforge3/envs/pandas-dev/bin/ninja
[1/1] Generating write_version_file with a custom command
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/__init__.py", line 138, in <module>
    from pandas import api, arrays, errors, io, plotting, tseries
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/api/__init__.py", line 2, in <module>
    from pandas.api import (
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/api/typing/__init__.py", line 31, in <module>
    from pandas.io.json._json import JsonReader
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/io/json/__init__.py", line 1, in <module>
    from pandas.io.json._json import (
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/io/json/_json.py", line 508, in <module>
    def read_json(
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/util/_decorators.py", line 379, in decorator
    params_applied = [
  File "/Users/linussommer/Documents/GitHub/pandas-linus-md/pandas/util/_decorators.py", line 380, in <listcomp>
    component.format(**params)
KeyError: '"index"'

even though the code runs otherwise.

Copy link
Contributor

@yuanx749 yuanx749 left a comment

Choose a reason for hiding this comment

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

To avoid the doc error, I guess double curly braces could be used here to escape {}.

pandas/io/json/_json.py Outdated Show resolved Hide resolved
pandas/io/json/_json.py Outdated Show resolved Hide resolved
@linus-md linus-md closed this Dec 19, 2023
@linus-md linus-md deleted the json-io branch December 19, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants