Skip to content

Commit 1b777aa

Browse files
committed
TST: test for #779, try to return string
1 parent 110be6f commit 1b777aa

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,13 @@ def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
10401040
formatter.to_string(force_unicode=force_unicode)
10411041

10421042
if buf is None:
1043-
return formatter.buf.getvalue()
1043+
result = formatter.buf.getvalue()
1044+
if not force_unicode:
1045+
try:
1046+
result = str(result)
1047+
except ValueError:
1048+
pass
1049+
return result
10441050

10451051
@Appender(fmt.docstring_to_string, indents=1)
10461052
def to_html(self, buf=None, columns=None, col_space=None, colSpace=None,

pandas/tests/test_format.py

+13
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ def test_to_string_float_formatting(self):
233233
'1 2.512000e-01')
234234
assert(df_s == expected)
235235

236+
def test_to_string_int_formatting(self):
237+
df = DataFrame({'x' : [-15, 20, 25, -35]})
238+
self.assert_(issubclass(df['x'].dtype.type, np.integer))
239+
240+
output = df.to_string()
241+
self.assert_(isinstance(output, str))
242+
expected = (' x\n'
243+
'0 -15\n'
244+
'1 20\n'
245+
'2 25\n'
246+
'3 -35')
247+
self.assertEqual(output, expected)
248+
236249
def test_to_string_left_justify_cols(self):
237250
fmt.reset_printoptions()
238251
df = DataFrame({'x' : [3234, 0.253]})

0 commit comments

Comments
 (0)