Skip to content

Commit 36345fd

Browse files
committed
API: Series/DataFrame.to_string return string by default. Series.to_string
buffer keyword namechange. addresses GH #232
1 parent 82d73a9 commit 36345fd

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pandas 0.5.0
1919
#225)
2020
- Removed `weights` option in panel regression which was not doing anything
2121
principled
22+
- Changed `buffer` argument name in `Series.to_string` to `buf`
23+
- `Series.to_string` and `DataFrame.to_string` now return strings by default
24+
instead of printing to sys.stdout
2225

2326
**New features / modules**
2427

pandas/core/frame.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,10 @@ def to_string(self, buf=None, columns=None, colSpace=None,
552552
sparsify=True):
553553
from pandas.core.common import _format, adjoin
554554

555+
return_ = False
555556
if buf is None: # pragma: no cover
556-
buf = sys.stdout
557+
buf = StringIO()
558+
return_ = True
557559

558560
if colSpace is None:
559561
def _myformat(v):
@@ -596,6 +598,9 @@ def _format_col(col):
596598
for s in to_write:
597599
print >> buf, s
598600

601+
if return_:
602+
return buf.getvalue()
603+
599604
def _get_formatted_labels(self, sparsify=True):
600605
from pandas.core.index import _sparsify
601606

@@ -3307,7 +3312,7 @@ def install_ipython_completers():
33073312
"""Register the DataFrame type with IPython's tab completion machinery, so
33083313
that it knows about accessing column names as attributes."""
33093314
from IPython.utils.generics import complete_object
3310-
3315+
33113316
@complete_object.when_type(DataFrame)
33123317
def complete_dataframe(obj, prev_completions):
33133318
return prev_completions + [c for c in obj.columns \

pandas/core/series.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,12 @@ def _tidy_repr(self, max_vals=20):
378378
result = '%s\nName: %s, Length: %d' % (result, self.name, len(self))
379379
return result
380380

381-
def to_string(self, buffer=sys.stdout, nanRep='NaN'):
382-
print >> buffer, self._get_repr(nanRep=nanRep)
381+
def to_string(self, buf=None, nanRep='NaN'):
382+
the_repr = self._get_repr(nanRep=nanRep)
383+
if buf is None:
384+
return the_repr
385+
else:
386+
print >> buf, the_repr
383387

384388
def _get_repr(self, name=False, nanRep='NaN'):
385389
vals = self.values

pandas/tests/test_frame.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ def test_getitem_boolean(self):
6767

6868
subframe_obj = self.tsframe[indexer_obj]
6969
assert_frame_equal(subframe_obj, subframe)
70-
70+
7171
def test_getattr(self):
7272
tm.assert_series_equal(self.frame.A, self.frame['A'])
73-
self.assertRaises(AttributeError, getattr, self.frame, 'NONEXISTENT_NAME')
73+
self.assertRaises(AttributeError, getattr, self.frame,
74+
'NONEXISTENT_NAME')
7475

7576
def test_setitem(self):
7677
# not sure what else to do here
@@ -1268,20 +1269,25 @@ def test_to_string(self):
12681269

12691270
biggie['A'][:20] = nan
12701271
biggie['B'][:20] = nan
1272+
s = biggie.to_string()
1273+
12711274
buf = StringIO()
1272-
biggie.to_string(buf=buf)
1275+
retval = biggie.to_string(buf=buf)
1276+
self.assert_(retval is None)
1277+
self.assertEqual(buf.getvalue(), s)
1278+
1279+
self.assert_(isinstance(s, basestring))
12731280

1274-
biggie.to_string(buf=buf, columns=['B', 'A'], colSpace=17)
1275-
biggie.to_string(buf=buf, columns=['B', 'A'],
1276-
formatters={'A' : lambda x: '%.1f' % x})
1281+
biggie.to_string(columns=['B', 'A'], colSpace=17)
1282+
biggie.to_string(columns=['B', 'A'],
1283+
formatters={'A' : lambda x: '%.1f' % x})
12771284

1278-
biggie.to_string(buf=buf, columns=['B', 'A'],
1279-
float_format=str)
1280-
biggie.to_string(buf=buf, columns=['B', 'A'], colSpace=12,
1281-
float_format=str)
1285+
biggie.to_string(columns=['B', 'A'], float_format=str)
1286+
biggie.to_string(columns=['B', 'A'], colSpace=12,
1287+
float_format=str)
12821288

12831289
frame = DataFrame(index=np.arange(1000))
1284-
frame.to_string(buf=buf)
1290+
frame.to_string()
12851291

12861292
def test_insert(self):
12871293
df = DataFrame(np.random.randn(5, 3), index=np.arange(5),

pandas/tests/test_series.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,13 @@ def test_repr(self):
426426

427427
def test_to_string(self):
428428
from cStringIO import StringIO
429-
self.ts.to_string(buffer=StringIO())
429+
buf = StringIO()
430+
431+
s = self.ts.to_string()
432+
433+
retval = self.ts.to_string(buf=buf)
434+
self.assert_(retval is None)
435+
self.assertEqual(buf.getvalue().strip(), s)
430436

431437
def test_iter(self):
432438
for i, val in enumerate(self.series):

0 commit comments

Comments
 (0)