Skip to content

REF: do slicing before calling Block.to_native_types #33248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 8 additions & 28 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,10 @@ def should_store(self, value: ArrayLike) -> bool:
"""
return is_dtype_equal(value.dtype, self.dtype)

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

if slicer is not None:
values = values[:, slicer]
mask = isna(values)
itemsize = writers.word_len(na_rep)

Expand Down Expand Up @@ -1715,11 +1713,9 @@ def get_values(self, dtype=None):
def array_values(self) -> ExtensionArray:
return self.values

def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
"""override to use ExtensionArray astype for the conversion"""
values = self.values
if slicer is not None:
values = values[slicer]
mask = isna(values)

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

def to_native_types(
self,
slicer=None,
na_rep="",
float_format=None,
decimal=".",
quoting=None,
**kwargs,
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs,
):
""" convert to our native types format, slicing if desired """
""" convert to our native types format """
values = self.values
if slicer is not None:
values = values[:, slicer]

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

return is_valid_nat_for_dtype(element, self.dtype)

def to_native_types(
self, slicer=None, na_rep=None, date_format=None, quoting=None, **kwargs
):
def to_native_types(self, na_rep=None, date_format=None, quoting=None, **kwargs):
""" convert to our native types format, slicing if desired """
values = self.values
i8values = self.values.view("i8")

if slicer is not None:
values = values[..., slicer]
i8values = i8values[..., slicer]

from pandas.io.formats.format import _get_format_datetime64_from_values

fmt = _get_format_datetime64_from_values(values, date_format)
Expand Down Expand Up @@ -2387,11 +2369,9 @@ def fillna(self, value, **kwargs):
)
return super().fillna(value, **kwargs)

def to_native_types(self, slicer=None, na_rep=None, quoting=None, **kwargs):
""" convert to our native types format, slicing if desired """
def to_native_types(self, na_rep=None, quoting=None, **kwargs):
""" convert to our native types format """
values = self.values
if slicer is not None:
values = values[:, slicer]
mask = isna(values)

rvalues = np.empty(values.shape, dtype=object)
Expand Down
11 changes: 7 additions & 4 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(

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

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

# create the data for a chunk
slicer = slice(start_i, end_i)
for i in range(len(self.blocks)):
b = self.blocks[i]

df = self.obj.iloc[slicer]
blocks = df._data.blocks

for i in range(len(blocks)):
b = blocks[i]
d = b.to_native_types(
slicer=slicer,
na_rep=self.na_rep,
float_format=self.float_format,
decimal=self.decimal,
Expand Down