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 2 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 @@ -588,7 +588,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 @@ -679,7 +679,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 @@ -1769,7 +1769,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 @@ -2016,7 +2016,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 @@ -2028,7 +2028,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 @@ -2187,7 +2188,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 @@ -2403,7 +2404,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
25 changes: 11 additions & 14 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,18 @@ def _save_chunk(self, start_i: int, end_i: int) -> None:
slicer = slice(start_i, end_i)

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

for i in range(len(blocks)):
b = blocks[i]
d = b.to_native_types(
na_rep=self.na_rep,
float_format=self.float_format,
decimal=self.decimal,
date_format=self.date_format,
quoting=self.quoting,
)
mgr = df._mgr

for col_loc, col in zip(b.mgr_locs, d):
# self.data is a preallocated list
self.data[col_loc] = col
res = mgr.apply(
"to_native_types",
na_rep=self.na_rep,
float_format=self.float_format,
decimal=self.decimal,
date_format=self.date_format,
quoting=self.quoting,
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: add a BlockManager.to_native_types, and put the above lines in there. That makes it easier to override this (that's what I am doing in the ArrayManager POC, and was thinking to split that out)

Copy link
Member Author

Choose a reason for hiding this comment

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

Go for it

Copy link
Member

Choose a reason for hiding this comment

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

I think that fits perfectly in this PR

for i in range(len(res.items)):
self.data[i] = res.iget_values(i)

ix = data_index.to_native_types(
slicer=slicer,
Expand Down