Skip to content

Commit ce50a85

Browse files
authored
REGR: to_json converting nullable ints to floats (#57232)
* REGR: to_json converting nullable ints to floats * Add skip
1 parent a7cd9b5 commit ce50a85

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

Diff for: 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`)

Diff for: pandas/core/arrays/arrow/array.py

+5
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,11 @@ def _to_timedeltaarray(self) -> TimedeltaArray:
13641364
np_array = np_array.astype(np_dtype)
13651365
return TimedeltaArray._simple_new(np_array, dtype=np_dtype)
13661366

1367+
def _values_for_json(self) -> np.ndarray:
1368+
if is_numeric_dtype(self.dtype):
1369+
return np.asarray(self, dtype=object)
1370+
return super()._values_for_json()
1371+
13671372
@doc(ExtensionArray.to_numpy)
13681373
def to_numpy(
13691374
self,

Diff for: pandas/core/arrays/masked.py

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

432432
# ------------------------------------------------------------------
433433

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

Diff for: pandas/tests/io/json/test_pandas.py

+16
Original file line numberDiff line numberDiff line change
@@ -2160,3 +2160,19 @@ def test_json_pos_args_deprecation():
21602160
with tm.assert_produces_warning(FutureWarning, match=msg):
21612161
buf = BytesIO()
21622162
df.to_json(buf, "split")
2163+
2164+
2165+
@td.skip_if_no("pyarrow")
2166+
def test_to_json_ea_null():
2167+
# GH#57224
2168+
df = DataFrame(
2169+
{
2170+
"a": Series([1, NA], dtype="int64[pyarrow]"),
2171+
"b": Series([2, NA], dtype="Int64"),
2172+
}
2173+
)
2174+
result = df.to_json(orient="records", lines=True)
2175+
expected = """{"a":1,"b":2}
2176+
{"a":null,"b":null}
2177+
"""
2178+
assert result == expected

0 commit comments

Comments
 (0)