Skip to content

REF: use BlockManager.to_native_types in formatting code #36417

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
Sep 17, 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
7 changes: 7 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,13 @@ def replace_list(
bm._consolidate_inplace()
return bm

def to_native_types(self, **kwargs) -> "BlockManager":
Copy link
Member

Choose a reason for hiding this comment

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

more informative name?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the name that the blocks also use..

Copy link
Member

Choose a reason for hiding this comment

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

It's not an informative name there either

Copy link
Member Author

Choose a reason for hiding this comment

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

Then I am tempted to say "Go for it" .. ;)

I am well aware and fully agree that the whole "native types" terminology is confusing, but it is used in quite some places (not only the Block method, but also the Index helper methods). So fully on board with changing it, but that's a bigger change than just trying to remove the internals leakage in the csv code what I am doing here.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah this can be updated / changed later

"""
Convert values to native types (strings / python objects) that are used
in formatting (repr / csv).
"""
return self.apply("to_native_types", **kwargs)

def is_consolidated(self) -> bool:
"""
Return True if more than one block with the same dtype
Expand Down
10 changes: 2 additions & 8 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,12 @@ def _save_body(self) -> None:
self._save_chunk(start_i, end_i)

def _save_chunk(self, start_i: int, end_i: int) -> None:
ncols = self.obj.shape[-1]
data = [None] * ncols

# create the data for a chunk
slicer = slice(start_i, end_i)

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

res = mgr.apply("to_native_types", **self._number_format)
for i in range(len(res.items)):
data[i] = res.iget_values(i)
res = df._mgr.to_native_types(**self._number_format)
data = [res.iget_values(i) for i in range(len(res.items))]

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