Skip to content

Commit

Permalink
REF: avoid internals in to_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Oct 8, 2023
1 parent bfdb4da commit 14a02d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,25 @@ def to_string(
line_width=line_width,
)

def _get_values_for_csv(
self,
*,
float_format: FloatFormatType | None,
date_format: str | None,
decimal: str,
na_rep: str,
quoting, # int csv.QUOTE_FOO from stdlib
) -> Self:
# helper used by to_csv
mgr = self._mgr.get_values_for_csv(
float_format=float_format,
date_format=date_format,
decimal=decimal,
na_rep=na_rep,
quoting=quoting,
)
return self._constructor_from_mgr(mgr, axes=mgr.axes)

# ----------------------------------------------------------------------

@property
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
slicer = slice(start_i, end_i)
df = self.obj.iloc[slicer]

res = df._mgr.get_values_for_csv(**self._number_format)
data = [res.iget_values(i) for i in range(len(res.items))]
res = df._get_values_for_csv(**self._number_format)
data = list(res._iter_column_arrays())

ix = self.data_index[slicer]._format_native_types(**self._number_format)
libwriters.write_csv_rows(
Expand Down

0 comments on commit 14a02d8

Please sign in to comment.