diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 3fd2a5e2bca32..d3ba14f93e9d9 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -44,6 +44,9 @@ from pandas.io.formats.format import DataFrameFormatter +_DEFAULT_CHUNKSIZE_CELLS = 100_000 + + class CSVFormatter: cols: np.ndarray @@ -162,7 +165,7 @@ def _initialize_columns(self, cols: Sequence[Hashable] | None) -> np.ndarray: def _initialize_chunksize(self, chunksize: int | None) -> int: if chunksize is None: - return (100000 // (len(self.cols) or 1)) or 1 + return (_DEFAULT_CHUNKSIZE_CELLS // (len(self.cols) or 1)) or 1 return int(chunksize) @property diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index b7874d51b6f33..a883e68331546 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -759,9 +759,11 @@ def test_to_csv_chunking(self, chunksize): tm.assert_frame_equal(rs, aa) @pytest.mark.slow - def test_to_csv_wide_frame_formatting(self): + def test_to_csv_wide_frame_formatting(self, monkeypatch): # Issue #8621 - df = DataFrame(np.random.randn(1, 100010), columns=None, index=None) + n_cells = 1_000 + monkeypatch.setattr("pandas.io.formats.csvs._DEFAULT_CHUNKSIZE_CELLS", n_cells) + df = DataFrame(np.random.randn(1, n_cells + 10), columns=None, index=None) with tm.ensure_clean() as filename: df.to_csv(filename, header=False, index=False) rs = read_csv(filename, header=None)