diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 3f446874ffd0e..865412f159ea1 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -631,6 +631,13 @@ def replace_list( bm._consolidate_inplace() return bm + def to_native_types(self, **kwargs) -> "BlockManager": + """ + Convert values to native types (strings / python objects) that are used + in formatting (repr / csv). + """ + return self.apply("to_native_types", **kwargs) + def is_consolidated(self) -> bool: """ Return True if more than one block with the same dtype diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 1bda16d126905..403eead686f89 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -334,18 +334,12 @@ def _save_body(self) -> None: self._save_chunk(start_i, end_i) def _save_chunk(self, start_i: int, end_i: int) -> None: - ncols = self.obj.shape[-1] - data = [None] * ncols - # create the data for a chunk slicer = slice(start_i, end_i) - df = self.obj.iloc[slicer] - mgr = df._mgr - res = mgr.apply("to_native_types", **self._number_format) - for i in range(len(res.items)): - data[i] = res.iget_values(i) + res = df._mgr.to_native_types(**self._number_format) + data = [res.iget_values(i) for i in range(len(res.items))] ix = self.data_index.to_native_types(slicer=slicer, **self._number_format) libwriters.write_csv_rows(data, ix, self.nlevels, self.cols, self.writer)