|
20 | 20 | is_float,
|
21 | 21 | is_numeric_dtype,
|
22 | 22 | is_datetime64_dtype,
|
23 |
| - is_timedelta64_dtype) |
| 23 | + is_timedelta64_dtype, |
| 24 | + is_list_like) |
24 | 25 | from pandas.types.generic import ABCSparseArray
|
25 |
| - |
26 | 26 | from pandas.core.base import PandasObject
|
27 | 27 | from pandas.core.index import Index, MultiIndex, _ensure_index
|
28 | 28 | from pandas import compat
|
|
54 | 54 | col_space : int, optional
|
55 | 55 | the minimum width of each column
|
56 | 56 | header : bool, optional
|
57 |
| - whether to print column labels, default True |
| 57 | + %(header)s |
58 | 58 | index : bool, optional
|
59 | 59 | whether to print index (row) labels, default True
|
60 | 60 | na_rep : string, optional
|
@@ -488,32 +488,38 @@ def _to_str_columns(self):
|
488 | 488 | # may include levels names also
|
489 | 489 |
|
490 | 490 | str_index = self._get_formatted_index(frame)
|
491 |
| - str_columns = self._get_formatted_column_labels(frame) |
492 | 491 |
|
493 |
| - if self.header: |
| 492 | + if not is_list_like(self.header) and not self.header: |
494 | 493 | stringified = []
|
495 | 494 | for i, c in enumerate(frame):
|
496 |
| - cheader = str_columns[i] |
497 |
| - max_colwidth = max(self.col_space or 0, *(self.adj.len(x) |
498 |
| - for x in cheader)) |
499 | 495 | fmt_values = self._format_col(i)
|
500 | 496 | fmt_values = _make_fixed_width(fmt_values, self.justify,
|
501 |
| - minimum=max_colwidth, |
| 497 | + minimum=(self.col_space or 0), |
502 | 498 | adj=self.adj)
|
503 |
| - |
504 |
| - max_len = max(np.max([self.adj.len(x) for x in fmt_values]), |
505 |
| - max_colwidth) |
506 |
| - cheader = self.adj.justify(cheader, max_len, mode=self.justify) |
507 |
| - stringified.append(cheader + fmt_values) |
| 499 | + stringified.append(fmt_values) |
508 | 500 | else:
|
| 501 | + if is_list_like(self.header): |
| 502 | + if len(self.header) != len(self.columns): |
| 503 | + raise ValueError(('Writing %d cols but got %d aliases' |
| 504 | + % (len(self.columns), len(self.header)))) |
| 505 | + str_columns = [[label] for label in self.header] |
| 506 | + else: |
| 507 | + str_columns = self._get_formatted_column_labels(frame) |
| 508 | + |
509 | 509 | stringified = []
|
510 | 510 | for i, c in enumerate(frame):
|
| 511 | + cheader = str_columns[i] |
| 512 | + header_colwidth = max(self.col_space or 0, |
| 513 | + *(self.adj.len(x) for x in cheader)) |
511 | 514 | fmt_values = self._format_col(i)
|
512 | 515 | fmt_values = _make_fixed_width(fmt_values, self.justify,
|
513 |
| - minimum=(self.col_space or 0), |
| 516 | + minimum=header_colwidth, |
514 | 517 | adj=self.adj)
|
515 | 518 |
|
516 |
| - stringified.append(fmt_values) |
| 519 | + max_len = max(np.max([self.adj.len(x) for x in fmt_values]), |
| 520 | + header_colwidth) |
| 521 | + cheader = self.adj.justify(cheader, max_len, mode=self.justify) |
| 522 | + stringified.append(cheader + fmt_values) |
517 | 523 |
|
518 | 524 | strcols = stringified
|
519 | 525 | if self.index:
|
|
0 commit comments