Skip to content

REF: use BlockManager.apply in csv code #36150

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 5 commits into from
Sep 12, 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
16 changes: 9 additions & 7 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def astype(self, dtype, copy: bool = False, errors: str = "raise"):

# use native type formatting for datetime/tz/timedelta
if self.is_datelike:
values = self.to_native_types()
values = self.to_native_types().values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.to_numpy() ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, to_native_types returns a Block


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

values[mask] = na_rep
return values
return self.make_block(values)

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

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

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

values[mask] = na_rep
return values
return self.make_block(values)

from pandas.io.formats.format import FloatArrayFormatter

Expand All @@ -2033,7 +2033,8 @@ def to_native_types(
quoting=quoting,
fixed_width=False,
)
return formatter.get_result_as_array()
res = formatter.get_result_as_array()
return self.make_block(res)


class ComplexBlock(FloatOrComplexBlock):
Expand Down Expand Up @@ -2192,7 +2193,7 @@ def to_native_types(self, na_rep="NaT", date_format=None, **kwargs):
result = dta._format_native_types(
na_rep=na_rep, date_format=date_format, **kwargs
)
return np.atleast_2d(result)
return self.make_block(result)

def set(self, locs, values):
"""
Expand Down Expand Up @@ -2408,7 +2409,8 @@ def fillna(self, value, **kwargs):
def to_native_types(self, na_rep="NaT", **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you now type all of these to return a Block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually adding this makes mypy complain about signature mismatch, rather not futz with it ATM

""" convert to our native types format """
tda = self.array_values()
return tda._format_native_types(na_rep, **kwargs)
res = tda._format_native_types(na_rep, **kwargs)
return self.make_block(res)


class BoolBlock(NumericBlock):
Expand Down
9 changes: 4 additions & 5 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,11 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
slicer = slice(start_i, end_i)

df = self.obj.iloc[slicer]
mgr = df._mgr

for block in df._mgr.blocks:
d = block.to_native_types(**self._number_format)

for col_loc, col in zip(block.mgr_locs, d):
data[col_loc] = col
res = mgr.apply("to_native_types", **self._number_format)
for i in range(len(res.items)):
data[i] = res.iget_values(i)

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