Skip to content

BUG: Parse missing values using read_json with dtype=False to NaN instead of None (GH28501) #37834

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
merged 5 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ I/O
- Bug in :func:`read_html` was raising a ``TypeError`` when supplying a ``pathlib.Path`` argument to the ``io`` parameter (:issue:`37705`)
- :meth:`to_excel` and :meth:`to_markdown` support writing to fsspec URLs such as S3 and Google Cloud Storage (:issue:`33987`)
- Bug in :meth:`read_fw` was not skipping blank lines (even with ``skip_blank_lines=True``) (:issue:`37758`)
- Parse missing values using :func:`read_json` with ``dtype=False`` to NaN instead of None (:issue:`28501`)
Copy link
Contributor

Choose a reason for hiding this comment

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

use double backticks around NaN and None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


Plotting
^^^^^^^^
Expand Down
7 changes: 5 additions & 2 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from pandas.core.dtypes.common import ensure_str, is_period_dtype

from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
from pandas import DataFrame, MultiIndex, Series, isna, notna, to_datetime
from pandas.core.construction import create_series_with_explicit_dtype
from pandas.core.generic import NDFrame
from pandas.core.reshape.concat import concat
Expand Down Expand Up @@ -861,7 +861,10 @@ def _try_convert_data(self, name, data, use_dtypes=True, convert_dates=True):
# don't try to coerce, unless a force conversion
if use_dtypes:
if not self.dtype:
return data, False
if all(notna(data)):
return data, False
return data.fillna(np.nan), True

elif self.dtype is True:
pass
else:
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,16 @@ def test_frame_from_json_missing_data(self, orient, convert_axes, numpy, dtype):
convert_axes=convert_axes,
dtype=dtype,
)
if not dtype: # TODO: Special case for object data; maybe a bug?
assert result.iloc[0, 2] is None
else:
assert np.isnan(result.iloc[0, 2])
assert np.isnan(result.iloc[0, 2])

@pytest.mark.parametrize("dtype", [True, False])
def test_frame_read_json_dtype_missing_value(self, orient, dtype):
# GH28501 Parse missing values using read_json with dtype=False
# to NaN instead of None
result = read_json("[null]", dtype=dtype)
expected = DataFrame([np.nan])

tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("inf", [np.inf, np.NINF])
@pytest.mark.parametrize("dtype", [True, False])
Expand Down