diff --git a/doc/source/whatsnew/v1.0.1.rst b/doc/source/whatsnew/v1.0.1.rst index 16d9d341f785d..305de5bbd57eb 100644 --- a/doc/source/whatsnew/v1.0.1.rst +++ b/doc/source/whatsnew/v1.0.1.rst @@ -10,6 +10,14 @@ including other versions of pandas. .. --------------------------------------------------------------------------- +.. _whatsnew_101.deprecations: + +Deprecations +~~~~~~~~~~~~ + +- Support for negative integer for :attr:`pd.options.display.max_colwidth` is deprecated in favor of using ``None`` (:issue:`31532`) + +.. --------------------------------------------------------------------------- .. _whatsnew_101.bug_fixes: @@ -129,6 +137,7 @@ ExtensionArray Other ^^^^^ - Regression fixed in objTOJSON.c fix return-type warning (:issue:`31463`) +- Fixed a regression where setting :attr:`pd.options.display.max_colwidth` was not accepting negative integer. In addition, this behavior has been deprecated in favor of using ``None`` (:issue:`31532`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index 3776c6f816d96..b0410e31c6de7 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -9,6 +9,8 @@ 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, @@ -341,8 +343,26 @@ 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 in future version. Instead, use None " + "to not limit the column width.", + FutureWarning, + stacklevel=4, + ) + cf.register_option( - "max_colwidth", 50, max_colwidth_doc, validator=is_nonnegative_int + # FIXME: change `validator=is_nonnegative_int` + # in version 1.2 + "max_colwidth", + 50, + max_colwidth_doc, + validator=is_instance_factory([type(None), int]), + cb=_deprecate_negative_int_max_colwidth, ) if is_terminal(): max_cols = 0 # automatically determine optimal number of columns diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 7650561d3072d..bf7b98eb78f11 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -239,6 +239,15 @@ 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 future version after deprecation cycle + # Non-regression test for: + # https://github.com/pandas-dev/pandas/issues/31532 + width = get_option("display.max_colwidth") + with tm.assert_produces_warning(FutureWarning): + set_option("display.max_colwidth", -1) + set_option("display.max_colwidth", width) + def test_repr_chop_threshold(self): df = DataFrame([[0.1, 0.5], [0.5, -0.1]]) pd.reset_option("display.chop_threshold") # default None