Skip to content

Commit 41f4862

Browse files
committed
BUG accept and deprecate negative integer for max_colwidth (pandas-dev#31569)
1 parent dc43d37 commit 41f4862

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/source/whatsnew/v1.0.1.rst

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ including other versions of pandas.
1010

1111
.. ---------------------------------------------------------------------------
1212
13+
.. _whatsnew_101.deprecations:
14+
15+
Deprecations
16+
~~~~~~~~~~~~
17+
18+
- Support for negative integer for :attr:`pd.options.display.max_colwidth` is deprecated in favor of using ``None`` (:issue:`31532`)
19+
20+
.. ---------------------------------------------------------------------------
1321
1422
.. _whatsnew_101.bug_fixes:
1523

@@ -128,6 +136,7 @@ ExtensionArray
128136
Other
129137
^^^^^
130138
- Regression fixed in objTOJSON.c fix return-type warning (:issue:`31463`)
139+
- 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`)
131140
-
132141

133142
.. ---------------------------------------------------------------------------

pandas/core/config_init.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
module is imported, register them here rather then in the module.
1010
1111
"""
12+
import warnings
13+
1214
import pandas._config.config as cf
1315
from pandas._config.config import (
1416
is_bool,
@@ -341,8 +343,26 @@ def is_terminal() -> bool:
341343
validator=is_instance_factory([type(None), int]),
342344
)
343345
cf.register_option("max_categories", 8, pc_max_categories_doc, validator=is_int)
346+
347+
def _deprecate_negative_int_max_colwidth(key):
348+
value = cf.get_option(key)
349+
if value is not None and value < 0:
350+
warnings.warn(
351+
"Passing a negative integer is deprecated in version 1.0 and "
352+
"will not be supported in future version. Instead, use None "
353+
"to not limit the column width.",
354+
FutureWarning,
355+
stacklevel=4,
356+
)
357+
344358
cf.register_option(
345-
"max_colwidth", 50, max_colwidth_doc, validator=is_nonnegative_int
359+
# FIXME: change `validator=is_nonnegative_int`
360+
# in version 1.2
361+
"max_colwidth",
362+
50,
363+
max_colwidth_doc,
364+
validator=is_instance_factory([type(None), int]),
365+
cb=_deprecate_negative_int_max_colwidth,
346366
)
347367
if is_terminal():
348368
max_cols = 0 # automatically determine optimal number of columns

pandas/tests/io/formats/test_format.py

+9
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ def test_repr_truncation(self):
239239
with option_context("display.max_colwidth", max_len + 2):
240240
assert "..." not in repr(df)
241241

242+
def test_repr_deprecation_negative_int(self):
243+
# FIXME: remove in future version after deprecation cycle
244+
# Non-regression test for:
245+
# https://github.com/pandas-dev/pandas/issues/31532
246+
width = get_option("display.max_colwidth")
247+
with tm.assert_produces_warning(FutureWarning):
248+
set_option("display.max_colwidth", -1)
249+
set_option("display.max_colwidth", width)
250+
242251
def test_repr_chop_threshold(self):
243252
df = DataFrame([[0.1, 0.5], [0.5, -0.1]])
244253
pd.reset_option("display.chop_threshold") # default None

0 commit comments

Comments
 (0)