Open jkornblum opened 7 years ago
yeah all of these validators are pretty simple, IOW for this one it allows just int
. It doesn't do anything more. Could enhance these by a) check that the value is >= 0 (0 is valid), maybe do some warning on certain ones).
This should be handled purely at the config level (and not at the actual display level).
I'm working on a commit and I've noticed quite a few more of these; all options for which negative values would be meaningless. Since I'm just starting to contribute I don't know if it's bad form to improve validation for these without being asked, but in order to help out I'll do these too. I could always provide a more limited commit if needed.
Some options (which would also be changed) currently provide exceptions such as: "ValueError: Value must be an instance of <type 'int'>|<type 'NoneType'>", which seems inelegant.
why don't you put up a prototype of what you are thinking. once one is good, can do the rest.
Just happened to PR it before I read your comment, apologies. I'll adjust as needed, let me know what you think.
take
This issue seems stale. What approach is desired to solve this? A quick solution could be to print a UserWarning for values of 3 or less. This can be implemented here as follows:
def _deprecate_negative_int_max_colwidth(key):
value = cf.get_option(key)
if value is not None and value < 0:
warnings.warn(
"Passing a negative integer is deprecated in version 1.0 and "
"will not be supported in future version. Instead, use None "
"to not limit the column width.",
FutureWarning,
stacklevel=find_stack_level(),
)
#<-- insert this
elif value < 4:
warnings.warn(
"Passing a value of 3 or less may have no effect on output.",
UserWarning,
stacklevel=find_stack_level(),
)
#-->
cf.register_option(
# TODO(2.0): change `validator=is_nonnegative_int` see GH#31569
"max_colwidth",
50,
max_colwidth_doc,
validator=is_instance_factory([type(None), int]),
cb=_deprecate_negative_int_max_colwidth,
)
After the introduction of the validator is_nonnegative_int
and recent changes to config_init.py
, the previous suggestion could be adapted as follows:
def _warn_max_colwidth_too_small(key):
value = cf.get_option(key)
if value < 4:
warnings.warn(
"A max_colwidth of 3 or less may have no effect on the output.",
UserWarning,
stacklevel=find_stack_level(),
)
cf.register_option(
"max_colwidth",
50,
max_colwidth_doc,
validator=is_nonnegative_int,
cb=_warn_max_colwidth_too_small
)
Unfortunately this makes the display somewhat redundant/unpleasant when visualizing a DataFrame. Using the initial example:
>>> pd.set_option('max_colwidth', 3)
<stdin>:1: UserWarning: Passing a value of 3 or less may have no effect on output.
>>> df
/usr/local/lib/python3.10/contextlib.py:135: UserWarning: Passing a value of 3 or less may have no effect on output.
return next(self.gen)
/usr/local/lib/python3.10/contextlib.py:142: UserWarning: Passing a value of 3 or less may have no effect on output.
next(self.gen)
0 1 2 3
0 foo bar bim uncomfortably long string
1 horse cow banana apple
When performing another type of operation, the display config is ignored.
>>> df[0]
0 foo
1 horse
Name: 0, dtype: object
Is there any other way to trigger warnings or tweak how they are displayed? Or would it make sense to just fall back to the suggestion from the initial message to update the documentation with the range?
Code Sample, a copy-pastable example if possible
Problem description
When I set the max_colwidth option to 3 or fewer it does not function and gives no warning. I assume that the ellipses take three characters so it has little meaning if you set the option less than 3. I think it should warn the user about the invalid setting or display an ellipse filled DataFrame.
Maybe the documentation could be updated to show range of 3 to xx.
Expected Output
Output of
pd.show_versions()