From 87540028c8915e2f4ba7c8a456a039ac363293f5 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 7 Jun 2022 14:30:38 -0700 Subject: [PATCH 1/2] Dperecate display.column_space --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/config_init.py | 12 +++++++++++- pandas/io/formats/format.py | 6 +----- pandas/tests/frame/test_repr_info.py | 2 +- pandas/tests/io/formats/test_format.py | 8 ++++++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 963acdc03d1f5..432af5eb4fbdd 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -686,6 +686,7 @@ Other Deprecations - Deprecated the ``closed`` argument in :class:`intervaltree` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated the ``closed`` argument in :class:`ArrowInterval` in favor of ``inclusive`` argument; In a future version passing ``closed`` will raise (:issue:`40245`) - Deprecated allowing ``unit="M"`` or ``unit="Y"`` in :class:`Timestamp` constructor with a non-round float value (:issue:`47267`) +- Deprecated the ``display.column_space`` global configuration option (:issue:`7576`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py index dd106b6dbb63c..47cf64ba24022 100644 --- a/pandas/core/config_init.py +++ b/pandas/core/config_init.py @@ -361,7 +361,17 @@ def is_terminal() -> bool: float_format_doc, validator=is_one_of_factory([None, is_callable]), ) - cf.register_option("column_space", 12, validator=is_int) + + def _deprecate_column_space(key): + warnings.warn( + "column_space is deprecated and will be removed " + "in a future version. Use df.to_string(col_space=...) " + "instead.", + FutureWarning, + stacklevel=find_stack_level(), + ) + + cf.register_option("column_space", 12, validator=is_int, cb=_deprecate_column_space) cf.register_option( "max_info_rows", 1690785, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 3019aa1fc2dc7..4c52166f7eb72 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1245,7 +1245,7 @@ def format_array( float_format: FloatFormatType | None = None, na_rep: str = "NaN", digits: int | None = None, - space: str | int | None = None, + space: str | int = 12, justify: str = "right", decimal: str = ".", leading_space: bool | None = True, @@ -1293,9 +1293,6 @@ def format_array( else: fmt_klass = GenericArrayFormatter - if space is None: - space = get_option("display.column_space") - if float_format is None: float_format = get_option("display.float_format") @@ -2099,7 +2096,6 @@ def set_eng_float_format(accuracy: int = 3, use_eng_prefix: bool = False) -> Non See also EngFormatter. """ set_option("display.float_format", EngFormatter(accuracy, use_eng_prefix)) - set_option("display.column_space", max(12, accuracy + 9)) def get_level_lengths( diff --git a/pandas/tests/frame/test_repr_info.py b/pandas/tests/frame/test_repr_info.py index 765640c94673e..f42660b297cb0 100644 --- a/pandas/tests/frame/test_repr_info.py +++ b/pandas/tests/frame/test_repr_info.py @@ -219,7 +219,7 @@ def test_repr_unsortable(self, float_frame): ) repr(unsortable) - fmt.set_option("display.precision", 3, "display.column_space", 10) + fmt.set_option("display.precision", 3) repr(float_frame) fmt.set_option("display.max_rows", 10, "display.max_columns", 2) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 04f4b0a313fdc..a7c9c86b3d9a5 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1442,8 +1442,6 @@ def test_to_string_float_formatting(self): fmt.set_option( "display.precision", 5, - "display.column_space", - 12, "display.notebook_repr_html", False, ) @@ -3402,3 +3400,9 @@ def test_filepath_or_buffer_bad_arg_raises(float_frame, method): msg = "buf is not a file name and it has no write method" with pytest.raises(TypeError, match=msg): getattr(float_frame, method)(buf=object()) + + +def test_col_space_deprecated(): + # GH 7576 + with tm.assert_produces_warning(FutureWarning, match="column_space is"): + set_option("display.column_space", 11) From 884442d74c1a9b169365bc3fe8e12f171887765b Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Tue, 7 Jun 2022 17:53:14 -0700 Subject: [PATCH 2/2] Address typing --- pandas/io/formats/format.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 4c52166f7eb72..045e74c1b6083 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1245,7 +1245,7 @@ def format_array( float_format: FloatFormatType | None = None, na_rep: str = "NaN", digits: int | None = None, - space: str | int = 12, + space: str | int | None = None, justify: str = "right", decimal: str = ".", leading_space: bool | None = True, @@ -1293,6 +1293,9 @@ def format_array( else: fmt_klass = GenericArrayFormatter + if space is None: + space = 12 + if float_format is None: float_format = get_option("display.float_format")