Skip to content

Commit dcb58b6

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#57232: REGR: to_json converting nullable ints to floats
1 parent 5034b78 commit dcb58b6

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
2121
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
2222
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
23+
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
2324
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
2425
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
2526
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)

pandas/core/arrays/arrow/array.py

+5
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,11 @@ def _to_timedeltaarray(self) -> TimedeltaArray:
13481348
np_array = np_array.astype(np_dtype)
13491349
return TimedeltaArray._simple_new(np_array, dtype=np_dtype)
13501350

1351+
def _values_for_json(self) -> np.ndarray:
1352+
if is_numeric_dtype(self.dtype):
1353+
return np.asarray(self, dtype=object)
1354+
return super()._values_for_json()
1355+
13511356
@doc(ExtensionArray.to_numpy)
13521357
def to_numpy(
13531358
self,

pandas/core/arrays/masked.py

+3
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ def __abs__(self) -> Self:
430430

431431
# ------------------------------------------------------------------
432432

433+
def _values_for_json(self) -> np.ndarray:
434+
return np.asarray(self, dtype=object)
435+
433436
def to_numpy(
434437
self,
435438
dtype: npt.DTypeLike | None = None,

pandas/tests/io/json/test_pandas.py

+16
Original file line numberDiff line numberDiff line change
@@ -2172,3 +2172,19 @@ def test_json_pos_args_deprecation():
21722172
with tm.assert_produces_warning(FutureWarning, match=msg):
21732173
buf = BytesIO()
21742174
df.to_json(buf, "split")
2175+
2176+
2177+
@td.skip_if_no("pyarrow")
2178+
def test_to_json_ea_null():
2179+
# GH#57224
2180+
df = DataFrame(
2181+
{
2182+
"a": Series([1, NA], dtype="int64[pyarrow]"),
2183+
"b": Series([2, NA], dtype="Int64"),
2184+
}
2185+
)
2186+
result = df.to_json(orient="records", lines=True)
2187+
expected = """{"a":1,"b":2}
2188+
{"a":null,"b":null}
2189+
"""
2190+
assert result == expected

0 commit comments

Comments
 (0)