Skip to content

Commit c104622

Browse files
authored
REF: use BlockManager.apply in csv code (#36150)
1 parent c6e3af7 commit c104622

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

pandas/core/internals/blocks.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):
593593

594594
# use native type formatting for datetime/tz/timedelta
595595
if self.is_datelike:
596-
values = self.to_native_types()
596+
values = self.to_native_types().values
597597

598598
# astype formatting
599599
else:
@@ -684,7 +684,7 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
684684
values = np.array(values, dtype="object")
685685

686686
values[mask] = na_rep
687-
return values
687+
return self.make_block(values)
688688

689689
# block actions #
690690
def copy(self, deep: bool = True):
@@ -1774,7 +1774,7 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
17741774

17751775
# TODO(EA2D): reshape not needed with 2D EAs
17761776
# we are expected to return a 2-d ndarray
1777-
return values.reshape(1, len(values))
1777+
return self.make_block(values)
17781778

17791779
def take_nd(
17801780
self, indexer, axis: int = 0, new_mgr_locs=None, fill_value=lib.no_default
@@ -2021,7 +2021,7 @@ def to_native_types(
20212021
values = np.array(values, dtype="object")
20222022

20232023
values[mask] = na_rep
2024-
return values
2024+
return self.make_block(values)
20252025

20262026
from pandas.io.formats.format import FloatArrayFormatter
20272027

@@ -2033,7 +2033,8 @@ def to_native_types(
20332033
quoting=quoting,
20342034
fixed_width=False,
20352035
)
2036-
return formatter.get_result_as_array()
2036+
res = formatter.get_result_as_array()
2037+
return self.make_block(res)
20372038

20382039

20392040
class ComplexBlock(FloatOrComplexBlock):
@@ -2192,7 +2193,7 @@ def to_native_types(self, na_rep="NaT", date_format=None, **kwargs):
21922193
result = dta._format_native_types(
21932194
na_rep=na_rep, date_format=date_format, **kwargs
21942195
)
2195-
return np.atleast_2d(result)
2196+
return self.make_block(result)
21962197

21972198
def set(self, locs, values):
21982199
"""
@@ -2408,7 +2409,8 @@ def fillna(self, value, **kwargs):
24082409
def to_native_types(self, na_rep="NaT", **kwargs):
24092410
""" convert to our native types format """
24102411
tda = self.array_values()
2411-
return tda._format_native_types(na_rep, **kwargs)
2412+
res = tda._format_native_types(na_rep, **kwargs)
2413+
return self.make_block(res)
24122414

24132415

24142416
class BoolBlock(NumericBlock):

pandas/io/formats/csvs.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,11 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
341341
slicer = slice(start_i, end_i)
342342

343343
df = self.obj.iloc[slicer]
344+
mgr = df._mgr
344345

345-
for block in df._mgr.blocks:
346-
d = block.to_native_types(**self._number_format)
347-
348-
for col_loc, col in zip(block.mgr_locs, d):
349-
data[col_loc] = col
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)
350349

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

0 commit comments

Comments
 (0)