Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def internal_values(self, dtype=None):
"""
return self.values

def formatting_values(self):
"""Return the internal format used by the DataFrame/SeriesFormatter"""
return self.internal_values()

def get_values(self, dtype=None):
"""
return an internal format, currently just the ndarray
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ def _values(self):
""" return the internal repr of this data """
return self._data.internal_values()

def _formatting_values(self):
"""Return the values that can be formatted (used by Series and
DataFrameFormatter)
"""
return self._data._block.formatting_values()
Copy link
Member Author

Choose a reason for hiding this comment

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

@jreback I could also add one extra level of redirection. Instead of now Series -> Block, I could also do Series -> SingleBlockManager -> Block. For that I just need to add a formatting_values to SingleBlockManager that calls Block.formatting_values(). But is seems an extra unneeded layer, although for internal_values or get_values it is done like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes I would add the indirection level


def get_values(self):
""" same as values (but handles sparseness conversions); is a view """
return self._data.get_values()
Expand Down
6 changes: 4 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ def _get_formatted_index(self):
return fmt_index, have_header

def _get_formatted_values(self):
return format_array(self.tr_series._values, None,
values_to_format = self.tr_series._formatting_values()
return format_array(values_to_format, None,
float_format=self.float_format, na_rep=self.na_rep)

def to_string(self):
Expand Down Expand Up @@ -694,7 +695,8 @@ def to_latex(self, column_format=None, longtable=False, encoding=None,
def _format_col(self, i):
frame = self.tr_frame
formatter = self._get_formatter(i)
return format_array(frame.iloc[:, i]._values, formatter,
values_to_format = frame.iloc[:, i]._formatting_values()
return format_array(values_to_format, formatter,
float_format=self.float_format, na_rep=self.na_rep,
space=self.col_space, decimal=self.decimal)

Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/test_internals_external_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# pylint: disable=W0102

import numpy as np

import pandas as pd
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe make a sub-dir for internals tests tests/internals/test_internals.py, test_block or some-such

from pandas.core.internals import Block, BlockManager


class CustomBlock(Block):

def formatting_values(self):
return np.array(["Val: {}".format(i) for i in self.values])


def test_custom_repr():
values = np.arange(3)

# series
block = CustomBlock(values, placement=slice(0, 3))
s = pd.Series(block, index=pd.RangeIndex(3), fastpath=True)
assert repr(s) == '0 Val: 0\n1 Val: 1\n2 Val: 2\ndtype: int64'

# dataframe
block = CustomBlock(values.reshape(1, -1), placement=slice(0, 1))
blk_mgr = BlockManager([block], [['col'], range(3)])
df = pd.DataFrame(blk_mgr)
assert repr(df) == ' col\n0 Val: 0\n1 Val: 1\n2 Val: 2'