Skip to content

Commit 407dea1

Browse files
jorisvandenbosscherhshadrach
authored andcommitted
REF: use BlockManager.to_native_types in formatting code (pandas-dev#36417)
1 parent 9dd987d commit 407dea1

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pandas/core/internals/managers.py

+7
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ def replace_list(
631631
bm._consolidate_inplace()
632632
return bm
633633

634+
def to_native_types(self, **kwargs) -> "BlockManager":
635+
"""
636+
Convert values to native types (strings / python objects) that are used
637+
in formatting (repr / csv).
638+
"""
639+
return self.apply("to_native_types", **kwargs)
640+
634641
def is_consolidated(self) -> bool:
635642
"""
636643
Return True if more than one block with the same dtype

pandas/io/formats/csvs.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,12 @@ def _save_body(self) -> None:
334334
self._save_chunk(start_i, end_i)
335335

336336
def _save_chunk(self, start_i: int, end_i: int) -> None:
337-
ncols = self.obj.shape[-1]
338-
data = [None] * ncols
339-
340337
# create the data for a chunk
341338
slicer = slice(start_i, end_i)
342-
343339
df = self.obj.iloc[slicer]
344-
mgr = df._mgr
345340

346-
res = mgr.apply("to_native_types", **self._number_format)
347-
for i in range(len(res.items)):
348-
data[i] = res.iget_values(i)
341+
res = df._mgr.to_native_types(**self._number_format)
342+
data = [res.iget_values(i) for i in range(len(res.items))]
349343

350344
ix = self.data_index.to_native_types(slicer=slicer, **self._number_format)
351345
libwriters.write_csv_rows(data, ix, self.nlevels, self.cols, self.writer)

0 commit comments

Comments
 (0)