Skip to content

Commit 5d9b513

Browse files
adamkleinwesm
authored andcommitted
PR#576 changes per wesm comments
1 parent 3085238 commit 5d9b513

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

pandas/core/common.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,10 @@ def set_eng_float_format(precision=3, use_eng_prefix=False):
491491
_float_format = EngFormatter(precision, use_eng_prefix)
492492
_column_space = max(12, precision + 9)
493493

494-
_float_format = lambda x: '% .4f' % x
494+
def _float_format(x):
495+
str_repr = '% .4g' % x
496+
return str_repr
497+
495498
_column_space = 12
496499
_max_rows = 500
497500
_max_columns = 0
@@ -503,7 +506,7 @@ def _stringify(col):
503506
else:
504507
return '%s' % col
505508

506-
def _format(s, space=None, na_rep=None, float_format=None):
509+
def _format(s, space=None, na_rep=None, float_format=None, col_width=None):
507510
def _just_help(x):
508511
if space is None:
509512
return x
@@ -520,6 +523,12 @@ def _just_help(x):
520523
else:
521524
formatted = _float_format(s)
522525

526+
# if we pass max_width, pad-zero the floats so all are same in column
527+
if col_width is not None and formatted != ' 0':
528+
padzeros = col_width - len(formatted)
529+
if padzeros > 0:
530+
formatted = formatted + ('0' * padzeros)
531+
523532
return _just_help(formatted)
524533
elif isinstance(s, int):
525534
return _just_help('% d' % s)

pandas/core/format.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,24 @@ def _get_column_formatter(self):
194194

195195
col_space = self.col_space
196196

197-
def _myformat(v):
198-
return _format(v, space=col_space, na_rep=self.na_rep,
199-
float_format=self.float_format)
197+
def _myformat(col):
198+
formatter = lambda v: _format(v, space=col_space,
199+
na_rep=self.na_rep,
200+
float_format=self.float_format)
201+
# one pass through when float to stringify column, to pad with
202+
# zeros
203+
if issubclass(col.dtype.type, np.floating):
204+
col_width = max(map(len, map(formatter, col)))
205+
formatter = lambda v: _format(v, space=col_space,
206+
na_rep=self.na_rep,
207+
float_format=self.float_format,
208+
col_width=col_width)
209+
return formatter
200210

201211
formatters = {} if self.formatters is None else self.formatters
202212

203213
def _format_col(col, i=None):
204-
formatter = formatters.get(col, _myformat)
214+
formatter = formatters.get(col, _myformat(self.frame[col]))
205215
if i == None:
206216
return [formatter(x) for x in self.frame[col]]
207217
else:
@@ -217,8 +227,7 @@ def _get_formatted_column_labels(self):
217227
formatters = {}
218228

219229
def is_numeric_dtype(dtype):
220-
return (issubclass(dtype.type, np.integer) or
221-
issubclass(dtype.type, np.floating))
230+
return issubclass(dtype.type, np.number)
222231

223232
if isinstance(self.columns, MultiIndex):
224233
fmt_columns = self.columns.format(sparsify=False, adjoin=False)

0 commit comments

Comments
 (0)