def side_by_side(*objs, **kwds): ''' created by wes mickinney, it only exists here becuase I use this function all the time. ''' from pandas.core.common import adjoin space = kwds.get('space', 4) reprs = [repr(obj).split('\n') for obj in objs] print adjoin(space, *reprs)
def to_string(self, force_unicode=False): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = ( u'Empty %s\nColumns: %s\nIndex: %s' % (type(self.frame).__name__, frame.columns, frame.index)) to_write.append(info_line) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [] for i, c in enumerate(self.columns): if self.header: fmt_values = self._format_col(i) cheader = str_columns[i] max_len = max(max(len(x) for x in fmt_values), max(len(x) for x in cheader)) if self.justify == 'left': cheader = [x.ljust(max_len) for x in cheader] else: cheader = [x.rjust(max_len) for x in cheader] fmt_values = cheader + fmt_values stringified.append( _make_fixed_width(fmt_values, self.justify)) else: stringified = [ _make_fixed_width(self._format_col(i), self.justify) for i, c in enumerate(self.columns) ] if self.index: to_write.append(adjoin(1, str_index, *stringified)) else: to_write.append(adjoin(1, *stringified)) if not py3compat.PY3: if force_unicode: to_write = [unicode(s) for s in to_write] else: # generally everything is plain strings, which has ascii # encoding. problem is when there is a char with value over 127 # - everything then gets converted to unicode. try: for s in to_write: str(s) except UnicodeError: to_write = [unicode(s) for s in to_write] self.buf.writelines(to_write)
def to_string(self, force_unicode=False): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = (u'Empty %s\nColumns: %s\nIndex: %s' % (type(self.frame).__name__, frame.columns, frame.index)) to_write.append(info_line) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [] for i, c in enumerate(self.columns): if self.header: fmt_values = self._format_col(i) cheader = str_columns[i] max_len = max(max(_strlen(x) for x in fmt_values), max(len(x) for x in cheader)) if self.justify == 'left': cheader = [x.ljust(max_len) for x in cheader] else: cheader = [x.rjust(max_len) for x in cheader] fmt_values = cheader + fmt_values stringified.append(_make_fixed_width(fmt_values, self.justify)) else: stringified = [_make_fixed_width(self._format_col(i), self.justify) for i, c in enumerate(self.columns)] if self.index: to_write.append(adjoin(1, str_index, *stringified)) else: to_write.append(adjoin(1, *stringified)) if not py3compat.PY3: if force_unicode: to_write = [unicode(s) for s in to_write] else: # generally everything is plain strings, which has ascii # encoding. problem is when there is a char with value over 127 # - everything then gets converted to unicode. try: for s in to_write: str(s) except UnicodeError: to_write = [unicode(s) for s in to_write] self.buf.writelines(to_write)
def to_string(self): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame format_col = self._get_column_formatter() to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = 'Empty %s\nColumns: %s\nIndex: %s' to_write.append(info_line % (type( self.frame).__name__, repr(frame.columns), repr(frame.index))) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [ str_columns[i] + format_col(c) for i, c in enumerate(self.columns) ] to_write.append(adjoin(1, str_index, *stringified)) for s in to_write: if isinstance(s, unicode): to_write = [unicode(s) for s in to_write] break self.buf.writelines(to_write)
def _join_multiline(self, *strcols): lwidth = self.line_width strcols = list(strcols) if self.index: idx = strcols.pop(0) lwidth -= np.array([len(x) for x in idx]).max() col_widths = [ np.array([len(x) for x in col]).max() if len(col) > 0 else 0 for col in strcols ] col_bins = _binify(col_widths, lwidth) nbins = len(col_bins) str_lst = [] st = 0 for i, ed in enumerate(col_bins): row = strcols[st:ed] row.insert(0, idx) if nbins > 1: if ed <= len(strcols) and i < nbins - 1: row.append([' \\'] + [' '] * (len(self.frame) - 1)) else: row.append([' '] * len(self.frame)) str_lst.append(adjoin(1, *row)) st = ed return '\n\n'.join(str_lst)
def to_string(self, force_unicode=None): """ Render a DataFrame to a console-friendly tabular output. """ import warnings if force_unicode is not None: # pragma: no cover warnings.warn( "force_unicode is deprecated, it will have no effect", FutureWarning) frame = self.frame if len(frame.columns) == 0 or len(frame.index) == 0: info_line = ( u'Empty %s\nColumns: %s\nIndex: %s' % (type(self.frame).__name__, com.pprint_thing( frame.columns), com.pprint_thing(frame.index))) text = info_line else: strcols = self._to_str_columns() if self.line_width is None: text = adjoin(1, *strcols) else: text = self._join_multiline(*strcols) self.buf.writelines(text)
def test_adjoin(self): data = [['a', 'b', 'c'], ['dd', 'ee', 'ff'], ['ggg', 'hhh', 'iii']] expected = 'a dd ggg\nb ee hhh\nc ff iii' adjoined = com.adjoin(2, *data) self.assertEqual(adjoined, expected)
def test_adjoin(): data = [["a", "b", "c"], ["dd", "ee", "ff"], ["ggg", "hhh", "iii"]] expected = "a dd ggg\nb ee hhh\nc ff iii" adjoined = com.adjoin(2, *data) assert adjoined == expected
def test_adjoin(): data = [['a', 'b', 'c'], ['dd', 'ee', 'ff'], ['ggg', 'hhh', 'iii']] expected = 'a dd ggg\nb ee hhh\nc ff iii' adjoined = com.adjoin(2, *data) assert (adjoined == expected)
def _get_formatted_index(self): # Note: this is only used by to_string(), not by to_html(). index = self.frame.index columns = self.frame.columns show_index_names = self.show_index_names and self.has_index_names show_col_names = (self.show_index_names and self.has_column_names) if isinstance(index, MultiIndex): fmt_index = index.format(sparsify=self.sparsify, adjoin=False, names=show_index_names) else: fmt_index = [index.format(name=show_index_names)] adjoined = adjoin(1, *fmt_index).split('\n') # empty space for columns if show_col_names: col_header = ['%s' % x for x in self._get_column_name_list()] else: col_header = [''] * columns.nlevels if self.header: return col_header + adjoined else: return adjoined
def test_adjoin_unicode(self): data = [[u'あ', 'b', 'c'], ['dd', u'ええ', 'ff'], ['ggg', 'hhh', u'いいい']] expected = u'あ dd ggg\nb ええ hhh\nc ff いいい' adjoined = com.adjoin(2, *data) self.assertEqual(adjoined, expected) adj = fmt.EastAsianTextAdjustment() expected = u"""あ dd ggg b ええ hhh c ff いいい""" adjoined = adj.adjoin(2, *data) self.assertEqual(adjoined, expected) cols = adjoined.split('\n') self.assertEqual(adj.len(cols[0]), 13) self.assertEqual(adj.len(cols[1]), 13) self.assertEqual(adj.len(cols[2]), 16) expected = u"""あ dd ggg b ええ hhh c ff いいい""" adjoined = adj.adjoin(7, *data) self.assertEqual(adjoined, expected) cols = adjoined.split('\n') self.assertEqual(adj.len(cols[0]), 23) self.assertEqual(adj.len(cols[1]), 23) self.assertEqual(adj.len(cols[2]), 26)
def to_string(self, force_unicode=None): """ Render a DataFrame to a console-friendly tabular output. """ import warnings if force_unicode is not None: # pragma: no cover warnings.warn("force_unicode is deprecated, it will have no effect", FutureWarning) frame = self.frame if len(frame.columns) == 0 or len(frame.index) == 0: info_line = (u'Empty %s\nColumns: %s\nIndex: %s' % (type(self.frame).__name__, com.pprint_thing(frame.columns), com.pprint_thing(frame.index))) text = info_line else: strcols = self._to_str_columns() if self.line_width is None: text = adjoin(1, *strcols) else: text = self._join_multiline(*strcols) self.buf.writelines(text)
def _get_formatted_index(self): index = self.frame.index columns = self.frame.columns show_index_names = self.show_index_names and self.has_index_names show_col_names = (self.show_index_names and self.has_column_names) if isinstance(index, MultiIndex): fmt_index = index.format(sparsify=self.sparsify, adjoin=False, names=show_index_names) else: fmt_index = [index.format(name=show_index_names)] adjoined = adjoin(1, *fmt_index).split('\n') # empty space for columns if show_col_names: col_header = ['%s' % x for x in self._get_column_name_list()] else: col_header = [''] * columns.nlevels if self.header: return col_header + adjoined else: return adjoined
def to_string(self): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame format_col = self._get_column_formatter() to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = "Empty %s\nColumns: %s\nIndex: %s" to_write.append(info_line % (type(self.frame).__name__, repr(frame.columns), repr(frame.index))) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [str_columns[i] + format_col(c) for i, c in enumerate(self.columns)] to_write.append(adjoin(1, str_index, *stringified)) for s in to_write: if isinstance(s, unicode): to_write = [unicode(s) for s in to_write] break self.buf.writelines(to_write)
def test_adjoin(): data = [['a', 'b', 'c'], ['dd', 'ee', 'ff'], ['ggg', 'hhh', 'iii']] expected = 'a dd ggg\nb ee hhh\nc ff iii' adjoined = common.adjoin(2, *data) assert (adjoined == expected)
def _join_multiline(self, *strcols): lwidth = self.line_width strcols = list(strcols) if self.index: idx = strcols.pop(0) lwidth -= np.array([len(x) for x in idx]).max() col_widths = [np.array([len(x) for x in col]).max() if len(col) > 0 else 0 for col in strcols] col_bins = _binify(col_widths, lwidth) nbins = len(col_bins) str_lst = [] st = 0 for i, ed in enumerate(col_bins): row = strcols[st:ed] row.insert(0, idx) if nbins > 1: if ed <= len(strcols) and i < nbins - 1: row.append([' \\'] + [' '] * (len(self.frame) - 1)) else: row.append([' '] * len(self.frame)) str_lst.append(adjoin(1, *row)) st = ed return '\n\n'.join(str_lst)
def _get_formatted_index(self): # Note: this is only used by to_string(), not by to_html(). index = self.frame.index columns = self.frame.columns show_index_names = self.show_index_names and self.has_index_names show_col_names = (self.show_index_names and self.has_column_names) fmt = self.formatters.get('__index__', None) if isinstance(index, MultiIndex): fmt_index = index.format(sparsify=self.sparsify, adjoin=False, names=show_index_names, formatter=fmt) else: fmt_index = [index.format(name=show_index_names, formatter=fmt)] adjoined = adjoin(1, *fmt_index).split('\n') # empty space for columns if show_col_names: col_header = ['%s' % x for x in self._get_column_name_list()] else: col_header = [''] * columns.nlevels if self.header: return col_header + adjoined else: return adjoined
def to_string(self): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = 'Empty %s\nColumns: %s\nIndex: %s' to_write.append(info_line % (type(self.frame).__name__, repr(frame.columns), repr(frame.index))) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [] for i, c in enumerate(self.columns): if self.header: fmt_values = self._format_col(c) cheader = str_columns[i] max_len = max(max(len(x) for x in fmt_values), max(len(x) for x in cheader)) if self.justify == 'left': cheader = [x.ljust(max_len) for x in cheader] else: cheader = [x.rjust(max_len) for x in cheader] stringified.append(cheader + fmt_values) else: stringified = [self._format_col(c) for c in self.columns] if self.index: to_write.append(adjoin(1, str_index, *stringified)) else: to_write.append(adjoin(1, *stringified)) for s in to_write: if isinstance(s, unicode): to_write = [unicode(s) for s in to_write] break self.buf.writelines(to_write)
def to_string(self): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame to_write = [] if len(frame.columns) == 0 or len(frame.index) == 0: info_line = 'Empty %s\nColumns: %s\nIndex: %s' to_write.append(info_line % (type( self.frame).__name__, repr(frame.columns), repr(frame.index))) else: # may include levels names also str_index = self._get_formatted_index() str_columns = self._get_formatted_column_labels() stringified = [] for i, c in enumerate(self.columns): if self.header: fmt_values = self._format_col(c) cheader = str_columns[i] max_len = max(max(len(x) for x in fmt_values), max(len(x) for x in cheader)) if self.justify == 'left': cheader = [x.ljust(max_len) for x in cheader] else: cheader = [x.rjust(max_len) for x in cheader] stringified.append(cheader + fmt_values) else: stringified = [self._format_col(c) for c in self.columns] if self.index: to_write.append(adjoin(1, str_index, *stringified)) else: to_write.append(adjoin(1, *stringified)) for s in to_write: if isinstance(s, unicode): to_write = [unicode(s) for s in to_write] break self.buf.writelines(to_write)
def to_string(self, force_unicode=False): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame if len(frame.columns) == 0 or len(frame.index) == 0: info_line = u"Empty %s\nColumns: %s\nIndex: %s" % (type(self.frame).__name__, frame.columns, frame.index) text = info_line else: strcols = self._to_str_columns(force_unicode) text = adjoin(1, *strcols) self.buf.writelines(text)
def to_string(self, force_unicode=False): """ Render a DataFrame to a console-friendly tabular output. """ frame = self.frame if len(frame.columns) == 0 or len(frame.index) == 0: info_line = ( u'Empty %s\nColumns: %s\nIndex: %s' % (type(self.frame).__name__, frame.columns, frame.index)) text = info_line else: strcols = self._to_str_columns(force_unicode) text = adjoin(1, *strcols) self.buf.writelines(text)
def __repr__(self): output = '%s\nFile path: %s\n' % (type(self), self.path) if len(self) > 0: keys = [] values = [] for k, v in sorted(self.handle.root._v_children.iteritems()): kind = v._v_attrs.pandas_type keys.append(str(k)) values.append(_NAME_MAP[kind]) output += adjoin(5, keys, values) else: output += 'Empty' return output
def _get_formatted_index(self): index = self.frame.index columns = self.frame.columns show_index_names = self.show_index_names and self.has_index_names show_col_names = self.show_index_names and self.has_column_names if isinstance(index, MultiIndex): fmt_index = index.format(sparsify=self.sparsify, adjoin=False, names=show_index_names) else: fmt_index = [index.format(name=show_index_names)] adjoined = adjoin(1, *fmt_index).split('\n') # empty space for columns if show_col_names: col_header = [' %s' % x for x in self._get_column_name_list()] else: col_header = [''] * columns.nlevels return col_header + adjoined
def side_by_side(*objs, **kwds): from pandas.core.common import adjoin space - kwds.get('space', 4) reprs = [repr(obj).split('\n') for obj in objs] print adjoin(space, *reprs)
def side_by_side(*objs, **kwds): # http://stackoverflow.com/questions/13030245/how-to-shift-a-pandas-multiindex-series from pandas.core.common import adjoin space = kwds.get('space', 4) reprs = [repr(obj).split('\n') for obj in objs] print adjoin(space, *reprs)
def side_by_side(*objs, **kwds): space = kwds.get('space', 4) reprs = [repr(obj).split('\n') for obj in objs] print adjoin(space, *reprs)
def side_by_side(*objs, **kwds): from pandas.core.common import adjoin space = kwds.get("space", 4) reprs = [repr(obj).split("\n") for obj in objs] print(adjoin(space, *reprs))