Skip to content

Commit 357c8a3

Browse files
Luke Shepardproost
Luke Shepard
authored andcommitted
Make DataFrame.to_string output full content by default (pandas-dev#28052)
1 parent 364e8af commit 357c8a3

File tree

8 files changed

+81
-26
lines changed

8 files changed

+81
-26
lines changed

doc/source/user_guide/options.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ display.max_colwidth 50 The maximum width in charac
353353
a column in the repr of a pandas
354354
data structure. When the column overflows,
355355
a "..." placeholder is embedded in
356-
the output.
356+
the output. 'None' value means unlimited.
357357
display.max_info_columns 100 max_info_columns is used in DataFrame.info
358358
method to decide if per column information
359359
will be printed.

doc/source/whatsnew/v1.0.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ including other versions of pandas.
2121
Enhancements
2222
~~~~~~~~~~~~
2323

24+
- :meth:`DataFrame.to_string` added the ``max_colwidth`` parameter to control when wide columns are truncated (:issue:`9784`)
25+
-
2426

2527
.. _whatsnew_1000.enhancements.other:
2628

@@ -191,6 +193,7 @@ I/O
191193
- Bug in :meth:`DataFrame.to_json` where using a Tuple as a column or index value and using ``orient="columns"`` or ``orient="index"`` would produce invalid JSON (:issue:`20500`)
192194
- Improve infinity parsing. :meth:`read_csv` now interprets ``Infinity``, ``+Infinity``, ``-Infinity`` as floating point values (:issue:`10065`)
193195
- Bug in :meth:`DataFrame.to_csv` where values were truncated when the length of ``na_rep`` was shorter than the text input data. (:issue:`25099`)
196+
- Bug in :func:`DataFrame.to_string` where values were truncated using display options instead of outputting the full content (:issue:`9784`)
194197

195198
Plotting
196199
^^^^^^^^

pandas/core/config_init.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ def use_numexpr_cb(key):
148148
"""
149149

150150
max_colwidth_doc = """
151-
: int
151+
: int or None
152152
The maximum width in characters of a column in the repr of
153153
a pandas data structure. When the column overflows, a "..."
154-
placeholder is embedded in the output.
154+
placeholder is embedded in the output. A 'None' value means unlimited.
155155
"""
156156

157157
colheader_justify_doc = """
@@ -340,7 +340,9 @@ def is_terminal():
340340
validator=is_instance_factory([type(None), int]),
341341
)
342342
cf.register_option("max_categories", 8, pc_max_categories_doc, validator=is_int)
343-
cf.register_option("max_colwidth", 50, max_colwidth_doc, validator=is_int)
343+
cf.register_option(
344+
"max_colwidth", 50, max_colwidth_doc, validator=is_nonnegative_int
345+
)
344346
if is_terminal():
345347
max_cols = 0 # automatically determine optimal number of columns
346348
else:

pandas/core/frame.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ def __repr__(self):
641641
max_rows = get_option("display.max_rows")
642642
min_rows = get_option("display.min_rows")
643643
max_cols = get_option("display.max_columns")
644+
max_colwidth = get_option("display.max_colwidth")
644645
show_dimensions = get_option("display.show_dimensions")
645646
if get_option("display.expand_frame_repr"):
646647
width, _ = console.get_console_size()
@@ -652,6 +653,7 @@ def __repr__(self):
652653
min_rows=min_rows,
653654
max_cols=max_cols,
654655
line_width=width,
656+
max_colwidth=max_colwidth,
655657
show_dimensions=show_dimensions,
656658
)
657659

@@ -730,12 +732,17 @@ def to_string(
730732
show_dimensions=False,
731733
decimal=".",
732734
line_width=None,
735+
max_colwidth=None,
733736
):
734737
"""
735738
Render a DataFrame to a console-friendly tabular output.
736739
%(shared_params)s
737740
line_width : int, optional
738741
Width to wrap a line in characters.
742+
max_colwidth : int, optional
743+
Max width to truncate each column in characters. By default, no limit.
744+
745+
.. versionadded:: 1.0.0
739746
%(returns)s
740747
See Also
741748
--------
@@ -752,26 +759,29 @@ def to_string(
752759
2 3 6
753760
"""
754761

755-
formatter = fmt.DataFrameFormatter(
756-
self,
757-
columns=columns,
758-
col_space=col_space,
759-
na_rep=na_rep,
760-
formatters=formatters,
761-
float_format=float_format,
762-
sparsify=sparsify,
763-
justify=justify,
764-
index_names=index_names,
765-
header=header,
766-
index=index,
767-
min_rows=min_rows,
768-
max_rows=max_rows,
769-
max_cols=max_cols,
770-
show_dimensions=show_dimensions,
771-
decimal=decimal,
772-
line_width=line_width,
773-
)
774-
return formatter.to_string(buf=buf)
762+
from pandas import option_context
763+
764+
with option_context("display.max_colwidth", max_colwidth):
765+
formatter = fmt.DataFrameFormatter(
766+
self,
767+
columns=columns,
768+
col_space=col_space,
769+
na_rep=na_rep,
770+
formatters=formatters,
771+
float_format=float_format,
772+
sparsify=sparsify,
773+
justify=justify,
774+
index_names=index_names,
775+
header=header,
776+
index=index,
777+
min_rows=min_rows,
778+
max_rows=max_rows,
779+
max_cols=max_cols,
780+
show_dimensions=show_dimensions,
781+
decimal=decimal,
782+
line_width=line_width,
783+
)
784+
return formatter.to_string(buf=buf)
775785

776786
# ----------------------------------------------------------------------
777787

pandas/io/clipboards.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
131131

132132
if isinstance(obj, ABCDataFrame):
133133
# str(df) has various unhelpful defaults, like truncation
134-
with option_context("display.max_colwidth", 999999):
134+
with option_context("display.max_colwidth", None):
135135
objstr = obj.to_string(**kwargs)
136136
else:
137137
objstr = str(obj)

pandas/io/formats/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def _write_header(self, indent: int) -> None:
377377
self.write("</thead>", indent)
378378

379379
def _get_formatted_values(self) -> Dict[int, List[str]]:
380-
with option_context("display.max_colwidth", 999999):
380+
with option_context("display.max_colwidth", None):
381381
fmt_values = {i: self.fmt._format_col(i) for i in range(self.ncols)}
382382
return fmt_values
383383

pandas/tests/config/test_config.py

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def test_validation(self):
218218
self.cf.set_option("a", 2) # int is_int
219219
self.cf.set_option("b.c", "wurld") # str is_str
220220
self.cf.set_option("d", 2)
221+
self.cf.set_option("d", None) # non-negative int can be None
221222

222223
# None not is_int
223224
with pytest.raises(ValueError, match=msg):

pandas/tests/io/formats/test_format.py

+39
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,45 @@ def test_str_max_colwidth(self):
527527
"1 foo bar stuff 1"
528528
)
529529

530+
def test_to_string_truncate(self):
531+
# GH 9784 - dont truncate when calling DataFrame.to_string
532+
df = pd.DataFrame(
533+
[
534+
{
535+
"a": "foo",
536+
"b": "bar",
537+
"c": "let's make this a very VERY long line that is longer "
538+
"than the default 50 character limit",
539+
"d": 1,
540+
},
541+
{"a": "foo", "b": "bar", "c": "stuff", "d": 1},
542+
]
543+
)
544+
df.set_index(["a", "b", "c"])
545+
assert df.to_string() == (
546+
" a b "
547+
" c d\n"
548+
"0 foo bar let's make this a very VERY long line t"
549+
"hat is longer than the default 50 character limit 1\n"
550+
"1 foo bar "
551+
" stuff 1"
552+
)
553+
with option_context("max_colwidth", 20):
554+
# the display option has no effect on the to_string method
555+
assert df.to_string() == (
556+
" a b "
557+
" c d\n"
558+
"0 foo bar let's make this a very VERY long line t"
559+
"hat is longer than the default 50 character limit 1\n"
560+
"1 foo bar "
561+
" stuff 1"
562+
)
563+
assert df.to_string(max_colwidth=20) == (
564+
" a b c d\n"
565+
"0 foo bar let's make this ... 1\n"
566+
"1 foo bar stuff 1"
567+
)
568+
530569
def test_auto_detect(self):
531570
term_width, term_height = get_terminal_size()
532571
fac = 1.05 # Arbitrary large factor to exceed term width

0 commit comments

Comments
 (0)