Skip to content

Commit 5353a7c

Browse files
committed
BUG: close 872, DataFrame.to_string formatters on integers
1 parent efd0c3c commit 5353a7c

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

pandas/core/format.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,14 @@ def get_result(self):
516516

517517
class IntArrayFormatter(GenericArrayFormatter):
518518

519-
520519
def get_result(self):
521-
fmt_values = ['% d' % x for x in self.values]
520+
if self.formatter:
521+
formatter = self.formatter
522+
else:
523+
formatter = lambda x: '% d' % x
524+
525+
fmt_values = [formatter(x) for x in self.values]
526+
522527
return _make_fixed_width(fmt_values, self.justify)
523528

524529

pandas/tests/test_format.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ def test_to_string_unicode_three(self):
8888
buf = StringIO()
8989
dm.to_string(buf)
9090

91+
def test_to_string_with_formatters(self):
92+
df = DataFrame({'int': [1, 2, 3],
93+
'float': [1.0, 2.0, 3.0],
94+
'object': [(1,2), True, False]},
95+
columns=['int', 'float', 'object'])
96+
97+
result = df.to_string(formatters={'int': lambda x: '0x%x' % x,
98+
'float': lambda x: '[% 4.1f]' % x,
99+
'object': lambda x: '-%s-' % str(x)})
100+
self.assertEqual(result, (' int float object\n'
101+
'0 0x1 [ 1.0] -(1, 2)-\n'
102+
'1 0x2 [ 2.0] -True-\n'
103+
'2 0x3 [ 3.0] -False-'))
104+
91105
def test_to_string_with_formatters_unicode(self):
92106
df = DataFrame({u'c/\u03c3':[1,2,3]})
93107
result = df.to_string(formatters={u'c/\u03c3': lambda x: '%s' % x})

0 commit comments

Comments
 (0)