Skip to content

Commit a99ec7d

Browse files
committed
tweaked toString output for DataFrame and DataMatrix
1 parent 093f4f4 commit a99ec7d

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

pandas/core/common.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ def _pfixed(s, space, nanRep=None, float_format=None):
8181
if float_format:
8282
formatted = float_format(s)
8383
else:
84-
formatted = '%.4g' % s
84+
is_pos = s >= 0
85+
formatted = '%.4g' % np.abs(s)
8586

86-
if formatted[0] != '-':
87+
if is_pos:
8788
formatted = ' ' + formatted
88-
89+
else:
90+
formatted = '-' + formatted
8991
return formatted.ljust(space)
9092
else:
9193
return ('%s' % s)[:space].ljust(space)

pandas/core/frame.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def toDataMatrix(self):
758758
from pandas.core.matrix import DataMatrix
759759
return DataMatrix(self._series, index=self.index)
760760

761-
def toString(self, buffer=sys.stdout, columns=None, colSpace=15,
761+
def toString(self, buffer=sys.stdout, columns=None, colSpace=None,
762762
nanRep='NaN', formatters=None, float_format=None):
763763
"""Output a tab-separated version of this DataFrame"""
764764
series = self._series
@@ -770,6 +770,18 @@ def toString(self, buffer=sys.stdout, columns=None, colSpace=15,
770770
formatters = formatters or {}
771771
ident = lambda x: x
772772

773+
if colSpace is None:
774+
colSpace = {}
775+
776+
for c in columns:
777+
if np.issctype(self[c].dtype):
778+
colSpace[c] = max(len(str(c)) + 4, 12)
779+
else:
780+
# hack
781+
colSpace[c] = 15
782+
else:
783+
colSpace = dict((k, 15) for k in columns)
784+
773785
if len(columns) == 0 or len(self.index) == 0:
774786
print >> buffer, 'Empty DataFrame'
775787
print >> buffer, repr(self.index)
@@ -778,16 +790,17 @@ def toString(self, buffer=sys.stdout, columns=None, colSpace=15,
778790
head = _pfixed('', idxSpace)
779791

780792
for h in columns:
781-
head += _pfixed(h, colSpace)
793+
head += _pfixed(h, colSpace[h])
782794

783795
print >> buffer, head
784796

785797
for idx in self.index:
786-
ot = _pfixed(idx, idxSpace)
798+
799+
ot = _pfixed(idx, idxSpace - 1)
787800
for k in columns:
788801
formatter = formatters.get(k, ident)
789802
ot += _pfixed(formatter(series[k][idx]),
790-
colSpace, nanRep=nanRep,
803+
colSpace[k], nanRep=nanRep,
791804
float_format=float_format)
792805
print >> buffer, ot
793806

pandas/core/matrix.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def _getSeriesDict(self):
704704
#-------------------------------------------------------------------------------
705705
# Outputting
706706

707-
def toString(self, buffer=sys.stdout, columns=None, colSpace=15,
707+
def toString(self, buffer=sys.stdout, columns=None, colSpace=None,
708708
nanRep='NaN', formatters=None, float_format=None):
709709
"""
710710
Output a string version of this DataMatrix
@@ -727,20 +727,32 @@ def toString(self, buffer=sys.stdout, columns=None, colSpace=15,
727727

728728
idxSpace = max([len(str(idx)) for idx in self.index]) + 4
729729

730+
if colSpace is None:
731+
colSpace = {}
732+
733+
for c in columns:
734+
if np.issctype(self[c].dtype):
735+
colSpace[c] = max(len(str(c)) + 4, 12)
736+
else:
737+
# hack
738+
colSpace[c] = 15
739+
else:
740+
colSpace = dict((k, 15) for k in columns)
741+
730742
if len(self.cols()) == 0:
731743
buffer.write('DataMatrix is empty!\n')
732744
buffer.write(repr(self.index))
733745
else:
734746
buffer.write(_pf('', idxSpace))
735747
for h in columns:
736-
buffer.write(_pf(h, colSpace))
748+
buffer.write(_pf(h, colSpace[h]))
737749
buffer.write('\n')
738750

739751
for i, idx in enumerate(self.index):
740-
buffer.write(_pf(idx, idxSpace))
752+
buffer.write(_pf(idx, idxSpace - 1))
741753
for j, col in enumerate(columns):
742754
formatter = formatters.get(col, ident)
743-
buffer.write(_pf(formatter(values[i, j]), colSpace,
755+
buffer.write(_pf(formatter(values[i, j]), colSpace[col],
744756
float_format=float_format,
745757
nanRep=nanRep))
746758
buffer.write('\n')

0 commit comments

Comments
 (0)