Skip to content

Commit 23d8051

Browse files
authored
REF: do slicing before calling Block.to_native_types (#33248)
1 parent cf9ec78 commit 23d8051

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

pandas/core/internals/blocks.py

+8-28
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,10 @@ def should_store(self, value: ArrayLike) -> bool:
651651
"""
652652
return is_dtype_equal(value.dtype, self.dtype)
653653

654-
def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
655-
""" convert to our native types format, slicing if desired """
654+
def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
655+
""" convert to our native types format """
656656
values = self.values
657657

658-
if slicer is not None:
659-
values = values[:, slicer]
660658
mask = isna(values)
661659
itemsize = writers.word_len(na_rep)
662660

@@ -1715,11 +1713,9 @@ def get_values(self, dtype=None):
17151713
def array_values(self) -> ExtensionArray:
17161714
return self.values
17171715

1718-
def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
1716+
def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
17191717
"""override to use ExtensionArray astype for the conversion"""
17201718
values = self.values
1721-
if slicer is not None:
1722-
values = values[slicer]
17231719
mask = isna(values)
17241720

17251721
values = np.asarray(values.astype(object))
@@ -1937,18 +1933,10 @@ def _can_hold_element(self, element: Any) -> bool:
19371933
)
19381934

19391935
def to_native_types(
1940-
self,
1941-
slicer=None,
1942-
na_rep="",
1943-
float_format=None,
1944-
decimal=".",
1945-
quoting=None,
1946-
**kwargs,
1936+
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs,
19471937
):
1948-
""" convert to our native types format, slicing if desired """
1938+
""" convert to our native types format """
19491939
values = self.values
1950-
if slicer is not None:
1951-
values = values[:, slicer]
19521940

19531941
# see gh-13418: no special formatting is desired at the
19541942
# output (important for appropriate 'quoting' behaviour),
@@ -2131,17 +2119,11 @@ def _can_hold_element(self, element: Any) -> bool:
21312119

21322120
return is_valid_nat_for_dtype(element, self.dtype)
21332121

2134-
def to_native_types(
2135-
self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs
2136-
):
2122+
def to_native_types(self, na_rep=None, date_format=None, quoting=None, **kwargs):
21372123
""" convert to our native types format, slicing if desired """
21382124
values = self.values
21392125
i8values = self.values.view("i8")
21402126

2141-
if slicer is not None:
2142-
values = values[..., slicer]
2143-
i8values = i8values[..., slicer]
2144-
21452127
from pandas.io.formats.format import _get_format_datetime64_from_values
21462128

21472129
fmt = _get_format_datetime64_from_values(values, date_format)
@@ -2387,11 +2369,9 @@ def fillna(self, value, **kwargs):
23872369
)
23882370
return super().fillna(value, **kwargs)
23892371

2390-
def to_native_types(self, slicer=None, na_rep=None, quoting=None, **kwargs):
2391-
""" convert to our native types format, slicing if desired """
2372+
def to_native_types(self, na_rep=None, quoting=None, **kwargs):
2373+
""" convert to our native types format """
23922374
values = self.values
2393-
if slicer is not None:
2394-
values = values[:, slicer]
23952375
mask = isna(values)
23962376

23972377
rvalues = np.empty(values.shape, dtype=object)

pandas/io/formats/csvs.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(
132132

133133
# preallocate data 2d list
134134
self.blocks = self.obj._data.blocks
135-
ncols = sum(b.shape[0] for b in self.blocks)
135+
ncols = self.obj.shape[-1]
136136
self.data = [None] * ncols
137137

138138
if chunksize is None:
@@ -327,10 +327,13 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
327327

328328
# create the data for a chunk
329329
slicer = slice(start_i, end_i)
330-
for i in range(len(self.blocks)):
331-
b = self.blocks[i]
330+
331+
df = self.obj.iloc[slicer]
332+
blocks = df._data.blocks
333+
334+
for i in range(len(blocks)):
335+
b = blocks[i]
332336
d = b.to_native_types(
333-
slicer=slicer,
334337
na_rep=self.na_rep,
335338
float_format=self.float_format,
336339
decimal=self.decimal,

0 commit comments

Comments
 (0)