Skip to content

Commit a984fed

Browse files
authored
REF: avoid accessing internals in io.formats.csv (#55446)
* REF: to_native_types->get_values_for_csv, make kwargs explicit * REF: avoid internals in to_csv
1 parent 288ab31 commit a984fed

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

pandas/core/frame.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,25 @@ def to_string(
13231323
line_width=line_width,
13241324
)
13251325

1326+
def _get_values_for_csv(
1327+
self,
1328+
*,
1329+
float_format: FloatFormatType | None,
1330+
date_format: str | None,
1331+
decimal: str,
1332+
na_rep: str,
1333+
quoting, # int csv.QUOTE_FOO from stdlib
1334+
) -> Self:
1335+
# helper used by to_csv
1336+
mgr = self._mgr.get_values_for_csv(
1337+
float_format=float_format,
1338+
date_format=date_format,
1339+
decimal=decimal,
1340+
na_rep=na_rep,
1341+
quoting=quoting,
1342+
)
1343+
return self._constructor_from_mgr(mgr, axes=mgr.axes)
1344+
13261345
# ----------------------------------------------------------------------
13271346

13281347
@property

pandas/core/internals/array_manager.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@
7979
ensure_block_shape,
8080
external_values,
8181
extract_pandas_array,
82+
get_values_for_csv,
8283
maybe_coerce_values,
8384
new_block,
84-
to_native_types,
8585
)
8686
from pandas.core.internals.managers import make_na_array
8787

@@ -343,8 +343,17 @@ def _convert(arr):
343343

344344
return self.apply(_convert)
345345

346-
def to_native_types(self, **kwargs) -> Self:
347-
return self.apply(to_native_types, **kwargs)
346+
def get_values_for_csv(
347+
self, *, float_format, date_format, decimal, na_rep: str = "nan", quoting=None
348+
) -> Self:
349+
return self.apply(
350+
get_values_for_csv,
351+
na_rep=na_rep,
352+
quoting=quoting,
353+
float_format=float_format,
354+
date_format=date_format,
355+
decimal=decimal,
356+
)
348357

349358
@property
350359
def any_extension_types(self) -> bool:

pandas/core/internals/blocks.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,18 @@ def astype(
692692
return newb
693693

694694
@final
695-
def to_native_types(self, na_rep: str = "nan", quoting=None, **kwargs) -> Block:
695+
def get_values_for_csv(
696+
self, *, float_format, date_format, decimal, na_rep: str = "nan", quoting=None
697+
) -> Block:
696698
"""convert to our native types format"""
697-
result = to_native_types(self.values, na_rep=na_rep, quoting=quoting, **kwargs)
699+
result = get_values_for_csv(
700+
self.values,
701+
na_rep=na_rep,
702+
quoting=quoting,
703+
float_format=float_format,
704+
date_format=date_format,
705+
decimal=decimal,
706+
)
698707
return self.make_block(result)
699708

700709
@final
@@ -2593,14 +2602,14 @@ def ensure_block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
25932602
return values
25942603

25952604

2596-
def to_native_types(
2605+
def get_values_for_csv(
25972606
values: ArrayLike,
25982607
*,
2608+
date_format,
25992609
na_rep: str = "nan",
26002610
quoting=None,
26012611
float_format=None,
26022612
decimal: str = ".",
2603-
**kwargs,
26042613
) -> npt.NDArray[np.object_]:
26052614
"""convert to our native types format"""
26062615
if isinstance(values, Categorical) and values.categories.dtype.kind in "Mm":
@@ -2615,14 +2624,16 @@ def to_native_types(
26152624

26162625
if isinstance(values, (DatetimeArray, TimedeltaArray)):
26172626
if values.ndim == 1:
2618-
result = values._format_native_types(na_rep=na_rep, **kwargs)
2627+
result = values._format_native_types(na_rep=na_rep, date_format=date_format)
26192628
result = result.astype(object, copy=False)
26202629
return result
26212630

26222631
# GH#21734 Process every column separately, they might have different formats
26232632
results_converted = []
26242633
for i in range(len(values)):
2625-
result = values[i, :]._format_native_types(na_rep=na_rep, **kwargs)
2634+
result = values[i, :]._format_native_types(
2635+
na_rep=na_rep, date_format=date_format
2636+
)
26262637
results_converted.append(result.astype(object, copy=False))
26272638
return np.vstack(results_converted)
26282639

pandas/core/internals/managers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,21 @@ def convert(self, copy: bool | None) -> Self:
430430

431431
return self.apply("convert", copy=copy, using_cow=using_copy_on_write())
432432

433-
def to_native_types(self, **kwargs) -> Self:
433+
def get_values_for_csv(
434+
self, *, float_format, date_format, decimal, na_rep: str = "nan", quoting=None
435+
) -> Self:
434436
"""
435437
Convert values to native types (strings / python objects) that are used
436438
in formatting (repr / csv).
437439
"""
438-
return self.apply("to_native_types", **kwargs)
440+
return self.apply(
441+
"get_values_for_csv",
442+
na_rep=na_rep,
443+
quoting=quoting,
444+
float_format=float_format,
445+
date_format=date_format,
446+
decimal=decimal,
447+
)
439448

440449
@property
441450
def any_extension_types(self) -> bool:

pandas/io/formats/csvs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
314314
slicer = slice(start_i, end_i)
315315
df = self.obj.iloc[slicer]
316316

317-
res = df._mgr.to_native_types(**self._number_format)
318-
data = [res.iget_values(i) for i in range(len(res.items))]
317+
res = df._get_values_for_csv(**self._number_format)
318+
data = list(res._iter_column_arrays())
319319

320320
ix = self.data_index[slicer]._format_native_types(**self._number_format)
321321
libwriters.write_csv_rows(

0 commit comments

Comments
 (0)