-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
REF: handling of max_colwidth parameter #25977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3245c0b
initial refactor
simonjayhawkins 2111d39
Merge remote-tracking branch 'upstream/master' into max_colwidth
simonjayhawkins b4a04d9
resolve merge conflicts
simonjayhawkins 86af9b9
resolve conflicts
simonjayhawkins 103fc2c
apply black code style to changes
simonjayhawkins a1fd43b
add docstring, type annotations and versionadded tag
simonjayhawkins 1362565
fix mypy errors
simonjayhawkins a5217ad
fix ImportError
simonjayhawkins 3a1fc5f
update annotation
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -370,6 +370,15 @@ def _get_formatter(self, i): | |
i = self.columns[i] | ||
return self.formatters.get(i, None) | ||
|
||
def _format_col(self, i): | ||
frame = self.tr_frame | ||
formatter = self._get_formatter(i) | ||
values_to_format = frame.iloc[:, i]._formatting_values() | ||
return format_array(values_to_format, formatter, | ||
float_format=self.float_format, na_rep=self.na_rep, | ||
space=self.col_space, decimal=self.decimal, | ||
max_colwidth=self.max_colwidth) | ||
|
||
|
||
class DataFrameFormatter(TableFormatter): | ||
""" | ||
|
@@ -433,6 +442,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None, | |
|
||
self._chk_truncate() | ||
self.adj = _get_adjustment() | ||
self.max_colwidth = None # use display.max_colwidth setting | ||
|
||
def _chk_truncate(self): | ||
""" | ||
|
@@ -708,14 +718,6 @@ def to_latex(self, column_format=None, longtable=False, encoding=None, | |
raise TypeError('buf is not a file name and it has no write ' | ||
'method') | ||
|
||
def _format_col(self, i): | ||
frame = self.tr_frame | ||
formatter = self._get_formatter(i) | ||
values_to_format = frame.iloc[:, i]._formatting_values() | ||
return format_array(values_to_format, formatter, | ||
float_format=self.float_format, na_rep=self.na_rep, | ||
space=self.col_space, decimal=self.decimal) | ||
|
||
def to_html(self, classes=None, notebook=False, border=None): | ||
""" | ||
Render a DataFrame to a html table. | ||
|
@@ -851,7 +853,7 @@ def _get_column_name_list(self): | |
|
||
def format_array(values, formatter, float_format=None, na_rep='NaN', | ||
digits=None, space=None, justify='right', decimal='.', | ||
leading_space=None): | ||
leading_space=None, max_colwidth=None): | ||
""" | ||
Format an array for printing. | ||
|
||
|
@@ -873,6 +875,11 @@ def format_array(values, formatter, float_format=None, na_rep='NaN', | |
When formatting an Index subclass | ||
(e.g. IntervalIndex._format_native_types), we don't want the | ||
leading space since it should be left-aligned. | ||
max_colwidth: False, int or None, optional, default None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a versionadded tag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not a public function. have added anyway. |
||
Whether the array should be formatted with strings truncated. | ||
* False: do not truncate strings | ||
* int: the maximum width of strings | ||
* None: use display.max_colwidth setting | ||
|
||
Returns | ||
------- | ||
|
@@ -903,10 +910,13 @@ def format_array(values, formatter, float_format=None, na_rep='NaN', | |
if digits is None: | ||
digits = get_option("display.precision") | ||
|
||
if max_colwidth is None: | ||
max_colwidth = get_option("display.max_colwidth") | ||
|
||
fmt_obj = fmt_klass(values, digits=digits, na_rep=na_rep, | ||
float_format=float_format, formatter=formatter, | ||
space=space, justify=justify, decimal=decimal, | ||
leading_space=leading_space) | ||
leading_space=leading_space, max_colwidth=max_colwidth) | ||
|
||
return fmt_obj.get_result() | ||
|
||
|
@@ -915,7 +925,8 @@ class GenericArrayFormatter: | |
|
||
def __init__(self, values, digits=7, formatter=None, na_rep='NaN', | ||
space=12, float_format=None, justify='right', decimal='.', | ||
quoting=None, fixed_width=True, leading_space=None): | ||
quoting=None, fixed_width=True, leading_space=None, | ||
max_colwidth=None): | ||
self.values = values | ||
self.digits = digits | ||
self.na_rep = na_rep | ||
|
@@ -927,10 +938,12 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN', | |
self.quoting = quoting | ||
self.fixed_width = fixed_width | ||
self.leading_space = leading_space | ||
self.max_colwidth = max_colwidth | ||
|
||
def get_result(self): | ||
fmt_values = self._format_strings() | ||
return _make_fixed_width(fmt_values, self.justify) | ||
return _make_fixed_width(fmt_values, self.justify, | ||
max_colwidth=self.max_colwidth) | ||
|
||
def _format_strings(self): | ||
if self.float_format is None: | ||
|
@@ -1395,7 +1408,8 @@ def _formatter(x): | |
return _formatter | ||
|
||
|
||
def _make_fixed_width(strings, justify='right', minimum=None, adj=None): | ||
def _make_fixed_width(strings, justify='right', minimum=None, adj=None, | ||
max_colwidth=None): | ||
|
||
if len(strings) == 0 or justify == 'all': | ||
return strings | ||
|
@@ -1408,13 +1422,12 @@ def _make_fixed_width(strings, justify='right', minimum=None, adj=None): | |
if minimum is not None: | ||
max_len = max(minimum, max_len) | ||
|
||
conf_max = get_option("display.max_colwidth") | ||
if conf_max is not None and max_len > conf_max: | ||
max_len = conf_max | ||
if max_colwidth is not None and max_len > max_colwidth: | ||
max_len = max_colwidth | ||
|
||
def just(x): | ||
if conf_max is not None: | ||
if (conf_max > 3) & (adj.len(x) > max_len): | ||
if max_colwidth is not None: | ||
if (max_colwidth > 3) & (adj.len(x) > max_len): | ||
x = x[:max_len - 3] + '...' | ||
return x | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.