Skip to content

Commit

Permalink
REF: repr - allow block to override values that get formatted (pandas…
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored and jowens committed Sep 20, 2017
1 parent 20487bf commit b4b4c77
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ def internal_values(self, dtype=None):
"""
return self.values

def formatting_values(self):
"""Return the internal values 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 Expand Up @@ -4317,6 +4321,10 @@ def external_values(self):
def internal_values(self):
return self._block.internal_values()

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

def get_values(self):
""" return a dense type view """
return np.array(self._block.to_dense(), copy=False)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,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 SeriesFormatter
and DataFrameFormatter)
"""
return self._data.formatting_values()

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
Empty file.
29 changes: 29 additions & 0 deletions pandas/tests/internals/test_external_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# pylint: disable=W0102

import numpy as np

import pandas as pd
from pandas.core.internals import Block, BlockManager, SingleBlockManager


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, dtype='int64')

# series
block = CustomBlock(values, placement=slice(0, 3))

s = pd.Series(SingleBlockManager(block, pd.RangeIndex(3)))
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'
File renamed without changes.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ def pxd(name):
'pandas.tests.indexes.datetimes',
'pandas.tests.indexes.timedeltas',
'pandas.tests.indexes.period',
'pandas.tests.internals',
'pandas.tests.io',
'pandas.tests.io.json',
'pandas.tests.io.parser',
Expand Down

0 comments on commit b4b4c77

Please sign in to comment.