Skip to content

Commit 16a3b01

Browse files
Backport PR #55691 on branch 2.1.x (REGR: fix read_parquet with column of large strings (avoid overflow from concat)) (#55706)
Backport PR #55691: REGR: fix read_parquet with column of large strings (avoid overflow from concat) Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
1 parent 9c7b567 commit 16a3b01

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Fixed regressions
2828
- Fixed regression in :meth:`DataFrameGroupBy.agg` and :meth:`SeriesGroupBy.agg` where if the option ``compute.use_numba`` was set to True, groupby methods not supported by the numba engine would raise a ``TypeError`` (:issue:`55520`)
2929
- Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
3030
- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
31+
- Fixed regression in :func:`read_parquet` when reading a file with a string column consisting of more than 2 GB of string data and using the ``"string"`` dtype (:issue:`55606`)
3132
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite when using ``detect_types`` (:issue:`55554`)
3233
- Fixed regression in construction of certain DataFrame or Series subclasses (:issue:`54922`)
3334

pandas/core/arrays/string_.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,19 @@ def __from_arrow__(
223223
# pyarrow.ChunkedArray
224224
chunks = array.chunks
225225

226+
results = []
227+
for arr in chunks:
228+
# convert chunk by chunk to numpy and concatenate then, to avoid
229+
# overflow for large string data when concatenating the pyarrow arrays
230+
arr = arr.to_numpy(zero_copy_only=False)
231+
arr = ensure_string_array(arr, na_value=libmissing.NA)
232+
results.append(arr)
233+
226234
if len(chunks) == 0:
227235
arr = np.array([], dtype=object)
228236
else:
229-
arr = pyarrow.concat_arrays(chunks).to_numpy(zero_copy_only=False)
230-
arr = ensure_string_array(arr, na_value=libmissing.NA)
237+
arr = np.concatenate(results)
238+
231239
# Bypass validation inside StringArray constructor, see GH#47781
232240
new_string_array = StringArray.__new__(StringArray)
233241
NDArrayBacked.__init__(

pandas/tests/io/test_parquet.py

+12
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,18 @@ def test_infer_string_large_string_type(self, tmp_path, pa):
11441144
)
11451145
tm.assert_frame_equal(result, expected)
11461146

1147+
# NOTE: this test is not run by default, because it requires a lot of memory (>5GB)
1148+
# @pytest.mark.slow
1149+
# def test_string_column_above_2GB(self, tmp_path, pa):
1150+
# # https://github.com/pandas-dev/pandas/issues/55606
1151+
# # above 2GB of string data
1152+
# v1 = b"x" * 100000000
1153+
# v2 = b"x" * 147483646
1154+
# df = pd.DataFrame({"strings": [v1] * 20 + [v2] + ["x"] * 20}, dtype="string")
1155+
# df.to_parquet(tmp_path / "test.parquet")
1156+
# result = read_parquet(tmp_path / "test.parquet")
1157+
# assert result["strings"].dtype == "string"
1158+
11471159

11481160
class TestParquetFastParquet(Base):
11491161
def test_basic(self, fp, df_full):

0 commit comments

Comments
 (0)