Skip to content

BUG accept and deprecate negative integer for max_colwidth #31569

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

Merged
merged 11 commits into from
Feb 2, 2020
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ExtensionArray

Other
^^^^^
-
- Fixed a regression where setting ``display.max_colwidth`` was not accepting negative integer. In addition, this behavior has been deprecated in favor of using ``None`` (:issue:`31532`)
-

.. ---------------------------------------------------------------------------
Expand Down
20 changes: 19 additions & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module is imported, register them here rather than in the module.

"""
import warnings
import pandas._config.config as cf
from pandas._config.config import (
is_bool,
Expand Down Expand Up @@ -341,8 +342,25 @@ def is_terminal() -> bool:
validator=is_instance_factory([type(None), int]),
)
cf.register_option("max_categories", 8, pc_max_categories_doc, validator=is_int)

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 from version 1.2. Instead, use None to "
"not limit the column width.",
FutureWarning,
)

cf.register_option(
"max_colwidth", 50, max_colwidth_doc, validator=is_nonnegative_int
# FIXME: change `validator=is_int` to `validator=is_nonnegative_int`
# in version 1.2
"max_colwidth",
50,
max_colwidth_doc,
validator=is_int,
cb=_deprecate_negative_int_max_colwidth,
)
if is_terminal():
max_cols = 0 # automatically determine optimal number of columns
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ def test_repr_truncation(self):
with option_context("display.max_colwidth", max_len + 2):
assert "..." not in repr(df)

def test_repr_deprecation_negative_int(self):
# FIXME: remove in version 1.2 after deprecation cycle
# Non-regression test for:
# https://github.com/pandas-dev/pandas/issues/31532
with pytest.warns(FutureWarning) as records:
set_option("display.max_colwidth", -1)
assert len(records) == 1
assert (
"Instead, use None to not limit the column width."
in records[0].message.args[0]
)

def test_repr_chop_threshold(self):
df = DataFrame([[0.1, 0.5], [0.5, -0.1]])
pd.reset_option("display.chop_threshold") # default None
Expand Down