From 5bfdc2154c0fa863c739b824454237c8004273b9 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Sat, 28 Mar 2020 10:25:27 +0700 Subject: [PATCH 01/18] add index kw and tests --- pandas/core/frame.py | 15 +++++++++++- pandas/tests/io/formats/test_to_markdown.py | 27 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 1e9f8995b6bed..38211f1ab3f0b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2091,10 +2091,23 @@ def to_feather(self, path) -> None: @Substitution(klass="DataFrame") @Appender(_shared_docs["to_markdown"]) def to_markdown( - self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs + self, + buf: Optional[IO[str]] = None, + mode: Optional[str] = None, + index: Optional[bool] = None, + **kwargs, ) -> Optional[str]: + if index is not None and "showindex" in kwargs: + warnings.warn( + "Index and showindex are aliases. Setting them both " + "causes undefined behaviour and could be " + "subjected to changes in the future.", + UserWarning, + stacklevel=3, + ) kwargs.setdefault("headers", "keys") kwargs.setdefault("tablefmt", "pipe") + kwargs.setdefault("showindex", True if index is None else index) tabulate = import_optional_dependency("tabulate") result = tabulate.tabulate(self, **kwargs) if buf is None: diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 8893e4294353f..c8c136b721925 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -53,3 +53,30 @@ def test_no_buf(capsys): assert ( result == "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" ) + + +@pytest.mark.parametrize("index", [True, False, None]) +@pytest.mark.parametrize("showindex", [True, False, None]) +def test_index(index, showindex): + kwargs = {} + if index is not None: + kwargs["index"] = index + if showindex is not None: + kwargs["showindex"] = showindex + + df = pd.DataFrame([1, 2, 3]) + + if "index" in kwargs and "showindex" in kwargs: + msg = "Index and showindex are aliases" + with pytest.warns(UserWarning, match=msg): + df.to_markdown(**kwargs) + else: + # only one or none is set + result = df.to_markdown(**kwargs) + if (index, showindex) in [(True, None), (None, True), (None, None)]: + assert result == ( + "| | 0 |\n|---:|----:|\n" + "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" + ) + else: + assert result == "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" From d74be05a4519a8d38aa25362a17bf2f57f8abbba Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Sat, 28 Mar 2020 10:34:50 +0700 Subject: [PATCH 02/18] add for series, edit docs --- pandas/core/generic.py | 2 ++ pandas/core/series.py | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5348040808e63..807149765bc92 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1903,6 +1903,8 @@ def _repr_data_resource_(self): Buffer to write to. If None, the output is returned as a string. mode : str, optional Mode in which file is opened. + index : bool, optional + Whether to print index (row) labels. **kwargs These parameters will be passed to `tabulate`. diff --git a/pandas/core/series.py b/pandas/core/series.py index 39e1178a3a5c3..6bc049e8a5aee 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1457,9 +1457,13 @@ def to_string( @Substitution(klass="Series") @Appender(generic._shared_docs["to_markdown"]) def to_markdown( - self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs + self, + buf: Optional[IO[str]] = None, + mode: Optional[str] = None, + index: Optional[bool] = None, + **kwargs, ) -> Optional[str]: - return self.to_frame().to_markdown(buf, mode, **kwargs) + return self.to_frame().to_markdown(buf, mode, index, **kwargs) # ---------------------------------------------------------------------- From decac71c3e05ce55ef366145c370e34f216b4c63 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 30 Mar 2020 11:33:31 +0700 Subject: [PATCH 03/18] add comment and doc --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/tests/io/formats/test_to_markdown.py | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 20415bba99476..79cdd88249142 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -71,6 +71,7 @@ Other enhancements - Positional slicing on a :class:`IntervalIndex` now supports slices with ``step > 1`` (:issue:`31658`) - :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`). - :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`) +- :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) - .. --------------------------------------------------------------------------- diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index c8c136b721925..4cfd8b8bdc65f 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -58,6 +58,7 @@ def test_no_buf(capsys): @pytest.mark.parametrize("index", [True, False, None]) @pytest.mark.parametrize("showindex", [True, False, None]) def test_index(index, showindex): + # GH 32667 kwargs = {} if index is not None: kwargs["index"] = index From 12b96a2b00ce06b02844ebc74a33b6e65e080c69 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 30 Mar 2020 14:15:21 +0700 Subject: [PATCH 04/18] change wanings to error --- pandas/core/frame.py | 9 ++------- pandas/tests/io/formats/test_to_markdown.py | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 38211f1ab3f0b..f3c27345d0ee6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2098,13 +2098,8 @@ def to_markdown( **kwargs, ) -> Optional[str]: if index is not None and "showindex" in kwargs: - warnings.warn( - "Index and showindex are aliases. Setting them both " - "causes undefined behaviour and could be " - "subjected to changes in the future.", - UserWarning, - stacklevel=3, - ) + raise ValueError("Index and showindex are aliases, please choose one") + kwargs.setdefault("headers", "keys") kwargs.setdefault("tablefmt", "pipe") kwargs.setdefault("showindex", True if index is None else index) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 4cfd8b8bdc65f..709a6e4058dc5 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -69,7 +69,7 @@ def test_index(index, showindex): if "index" in kwargs and "showindex" in kwargs: msg = "Index and showindex are aliases" - with pytest.warns(UserWarning, match=msg): + with pytest.raises(ValueError, match=msg): df.to_markdown(**kwargs) else: # only one or none is set From ff0b9700ea1025386d49874200ac9845477ff3fb Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 6 Apr 2020 12:34:03 +0700 Subject: [PATCH 05/18] completely remove showindex kw --- pandas/core/frame.py | 9 ++++++--- pandas/core/generic.py | 5 ++++- pandas/core/series.py | 2 +- pandas/tests/io/formats/test_to_markdown.py | 11 +++++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f3c27345d0ee6..5f6a92e186dda 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2094,11 +2094,14 @@ def to_markdown( self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, - index: Optional[bool] = None, + index: Optional[bool] = True, **kwargs, ) -> Optional[str]: - if index is not None and "showindex" in kwargs: - raise ValueError("Index and showindex are aliases, please choose one") + if "showindex" in kwargs: + raise ValueError( + "'showindex' is not a valid keyword argument, " + "please use 'index' to control showing the index" + ) kwargs.setdefault("headers", "keys") kwargs.setdefault("tablefmt", "pipe") diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 807149765bc92..99f3d59561f8b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1903,8 +1903,11 @@ def _repr_data_resource_(self): Buffer to write to. If None, the output is returned as a string. mode : str, optional Mode in which file is opened. - index : bool, optional + index : bool, default True Whether to print index (row) labels. + + .. versionadded:: 1.1 + **kwargs These parameters will be passed to `tabulate`. diff --git a/pandas/core/series.py b/pandas/core/series.py index 6bc049e8a5aee..059d41db7801f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1460,7 +1460,7 @@ def to_markdown( self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, - index: Optional[bool] = None, + index: Optional[bool] = True, **kwargs, ) -> Optional[str]: return self.to_frame().to_markdown(buf, mode, index, **kwargs) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 709a6e4058dc5..50582498d0acf 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -67,14 +67,17 @@ def test_index(index, showindex): df = pd.DataFrame([1, 2, 3]) - if "index" in kwargs and "showindex" in kwargs: - msg = "Index and showindex are aliases" + if "showindex" in kwargs: + # force user to use index instead of tabulate's show index + msg = ( + "'showindex' is not a valid keyword argument, " + "please use 'index' to control showing the index" + ) with pytest.raises(ValueError, match=msg): df.to_markdown(**kwargs) else: - # only one or none is set result = df.to_markdown(**kwargs) - if (index, showindex) in [(True, None), (None, True), (None, None)]: + if index in [True, None]: assert result == ( "| | 0 |\n|---:|----:|\n" "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" From e519fa340db7cf967c233145e2a06f2ff6336f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quang=20Nguy=E1=BB=85n?= <30631476+quangngd@users.noreply.github.com> Date: Sat, 9 May 2020 08:46:38 +0700 Subject: [PATCH 06/18] Update v1.1.0.rst --- doc/source/whatsnew/v1.1.0.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 6f42580e3be30..454659be08c2b 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -150,9 +150,8 @@ Other enhancements such as ``dict`` and ``list``, mirroring the behavior of :meth:`DataFrame.update` (:issue:`33215`) - :meth:`~pandas.core.groupby.GroupBy.transform` and :meth:`~pandas.core.groupby.GroupBy.aggregate` has gained ``engine`` and ``engine_kwargs`` arguments that supports executing functions with ``Numba`` (:issue:`32854`, :issue:`33388`) - :meth:`~pandas.core.resample.Resampler.interpolate` now supports SciPy interpolation method :class:`scipy.interpolate.CubicSpline` as method ``cubicspline`` (:issue:`33670`) -- :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) -- :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) - :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) +- :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) - .. --------------------------------------------------------------------------- From 8cc92c1e95615d1de883967c20172a20d6acfd23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quang=20Nguy=E1=BB=85n?= <30631476+quangngd@users.noreply.github.com> Date: Sat, 9 May 2020 08:47:32 +0700 Subject: [PATCH 07/18] Update v1.1.0.rst --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 454659be08c2b..9bdf3826d764e 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -150,7 +150,6 @@ Other enhancements such as ``dict`` and ``list``, mirroring the behavior of :meth:`DataFrame.update` (:issue:`33215`) - :meth:`~pandas.core.groupby.GroupBy.transform` and :meth:`~pandas.core.groupby.GroupBy.aggregate` has gained ``engine`` and ``engine_kwargs`` arguments that supports executing functions with ``Numba`` (:issue:`32854`, :issue:`33388`) - :meth:`~pandas.core.resample.Resampler.interpolate` now supports SciPy interpolation method :class:`scipy.interpolate.CubicSpline` as method ``cubicspline`` (:issue:`33670`) -- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`) - :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) - From 492f72ff47b30a701b532f18701b13c3290d2869 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Thu, 18 Jun 2020 15:15:39 +0700 Subject: [PATCH 08/18] change ValueError to FutureWarning --- pandas/core/frame.py | 10 +++++++--- pandas/tests/io/formats/test_to_markdown.py | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a7cab266c1d90..05aa67c27568e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2141,9 +2141,13 @@ def to_markdown( **kwargs, ) -> Optional[str]: if "showindex" in kwargs: - raise ValueError( - "'showindex' is not a valid keyword argument, " - "please use 'index' to control showing the index" + index = kwargs["showindex"] + del kwargs["showindex"] + + warnings.warn( + "'showindex' is deprecated. Only 'index' will be used " + "in a future version. Use 'index' to silence this warning.", + FutureWarning, ) kwargs.setdefault("headers", "keys") diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 50582498d0acf..4ddf8ce51357e 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -68,13 +68,18 @@ def test_index(index, showindex): df = pd.DataFrame([1, 2, 3]) if "showindex" in kwargs: - # force user to use index instead of tabulate's show index - msg = ( - "'showindex' is not a valid keyword argument, " - "please use 'index' to control showing the index" - ) - with pytest.raises(ValueError, match=msg): - df.to_markdown(**kwargs) + msg = "deprecated" + with pytest.warns(FutureWarning, match=msg): + result = df.to_markdown(**kwargs) + + # give showindex higher priority if specified + if showindex: + assert result == ( + "| | 0 |\n|---:|----:|\n" + "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" + ) + else: + assert result == "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" else: result = df.to_markdown(**kwargs) if index in [True, None]: From 5670f670d77750ad56835258f78805ade76f062b Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 1 Jul 2020 11:24:38 +0700 Subject: [PATCH 09/18] fix warnings --- pandas/core/frame.py | 1 + pandas/tests/io/formats/test_to_markdown.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8f363fc25b9dd..41c1b8fea0e54 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2230,6 +2230,7 @@ def to_markdown( "'showindex' is deprecated. Only 'index' will be used " "in a future version. Use 'index' to silence this warning.", FutureWarning, + stacklevel=2, ) kwargs.setdefault("headers", "keys") diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 4ddf8ce51357e..3a75d1fe82b30 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -3,6 +3,8 @@ import pytest import pandas as pd +import pandas._testing as tm + pytest.importorskip("tabulate") @@ -68,8 +70,7 @@ def test_index(index, showindex): df = pd.DataFrame([1, 2, 3]) if "showindex" in kwargs: - msg = "deprecated" - with pytest.warns(FutureWarning, match=msg): + with tm.assert_produces_warning(FutureWarning): result = df.to_markdown(**kwargs) # give showindex higher priority if specified From e80230a16f4151ae5e04fbd9690ddc37ef0fa1e2 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 1 Jul 2020 11:58:49 +0700 Subject: [PATCH 10/18] fix import --- pandas/tests/io/formats/test_to_markdown.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 3a75d1fe82b30..05425c614ded6 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -5,7 +5,6 @@ import pandas as pd import pandas._testing as tm - pytest.importorskip("tabulate") From 992f923ad630a1b5a26881a87ea0540473afb9ec Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Thu, 2 Jul 2020 15:05:59 +0700 Subject: [PATCH 11/18] refactor test, fix typing and doc --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/core/frame.py | 4 ++-- pandas/core/series.py | 2 +- pandas/tests/io/formats/test_to_markdown.py | 18 ++++++++---------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ac94934f38306..55e9aeec26c61 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -328,7 +328,7 @@ Other enhancements - :meth:`DataFrame.to_html` and :meth:`DataFrame.to_string`'s ``col_space`` parameter now accepts a list or dict to change only some specific columns' width (:issue:`28917`). - :meth:`DataFrame.to_excel` can now also write OpenOffice spreadsheet (.ods) files (:issue:`27222`) - :meth:`~Series.explode` now accepts ``ignore_index`` to reset the index, similarly to :meth:`pd.concat` or :meth:`DataFrame.sort_values` (:issue:`34932`). -- :meth:`DataFrame.to_markdown` will now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) +- :meth:`DataFrame.to_markdown` and :meth:`Series.to_markdown` now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 41c1b8fea0e54..c65c476a6ca51 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2219,7 +2219,7 @@ def to_markdown( self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, - index: Optional[bool] = True, + index: bool = True, **kwargs, ) -> Optional[str]: if "showindex" in kwargs: @@ -2235,7 +2235,7 @@ def to_markdown( kwargs.setdefault("headers", "keys") kwargs.setdefault("tablefmt", "pipe") - kwargs.setdefault("showindex", True if index is None else index) + kwargs.setdefault("showindex", index) tabulate = import_optional_dependency("tabulate") result = tabulate.tabulate(self, **kwargs) if buf is None: diff --git a/pandas/core/series.py b/pandas/core/series.py index 91c1a49837e0f..24b7e8e486547 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1426,7 +1426,7 @@ def to_markdown( self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, - index: Optional[bool] = True, + index: bool = True, **kwargs, ) -> Optional[str]: """ diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 05425c614ded6..6e8d16a05fd56 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -67,6 +67,10 @@ def test_index(index, showindex): kwargs["showindex"] = showindex df = pd.DataFrame([1, 2, 3]) + yes_index_result = ( + "| | 0 |\n|---:|----:|\n" "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" + ) + no_index_result = "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" if "showindex" in kwargs: with tm.assert_produces_warning(FutureWarning): @@ -74,18 +78,12 @@ def test_index(index, showindex): # give showindex higher priority if specified if showindex: - assert result == ( - "| | 0 |\n|---:|----:|\n" - "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" - ) + assert result == yes_index_result else: - assert result == "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" + assert result == no_index_result else: result = df.to_markdown(**kwargs) if index in [True, None]: - assert result == ( - "| | 0 |\n|---:|----:|\n" - "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" - ) + assert result == yes_index_result else: - assert result == "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" + assert result == no_index_result From 68cf1f7f2595128325e222e98cbd36854a2cedac Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Thu, 2 Jul 2020 15:27:29 +0700 Subject: [PATCH 12/18] fix blacks split line --- pandas/tests/io/formats/test_to_markdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index 6e8d16a05fd56..a46b9b467a7d3 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -68,7 +68,7 @@ def test_index(index, showindex): df = pd.DataFrame([1, 2, 3]) yes_index_result = ( - "| | 0 |\n|---:|----:|\n" "| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" + "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |" ) no_index_result = "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" From 9ee640f2b965d90c26da287ddb2a89463ffba563 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Tue, 7 Jul 2020 10:01:25 +0700 Subject: [PATCH 13/18] refactor test --- pandas/tests/io/formats/test_to_markdown.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index a46b9b467a7d3..fcf05874595bc 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -72,17 +72,17 @@ def test_index(index, showindex): ) no_index_result = "| 0 |\n|----:|\n| 1 |\n| 2 |\n| 3 |" - if "showindex" in kwargs: - with tm.assert_produces_warning(FutureWarning): - result = df.to_markdown(**kwargs) + warning = FutureWarning if "showindex" in kwargs else None + with tm.assert_produces_warning(warning): + result = df.to_markdown(**kwargs) + if "showindex" in kwargs: # give showindex higher priority if specified if showindex: assert result == yes_index_result else: assert result == no_index_result else: - result = df.to_markdown(**kwargs) if index in [True, None]: assert result == yes_index_result else: From 82bb8229243fdec5a8b843722c2a679339804496 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Tue, 7 Jul 2020 15:57:41 +0700 Subject: [PATCH 14/18] refactor test --- pandas/tests/io/formats/test_to_markdown.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_to_markdown.py b/pandas/tests/io/formats/test_to_markdown.py index fcf05874595bc..5223b313fef4f 100644 --- a/pandas/tests/io/formats/test_to_markdown.py +++ b/pandas/tests/io/formats/test_to_markdown.py @@ -79,11 +79,12 @@ def test_index(index, showindex): if "showindex" in kwargs: # give showindex higher priority if specified if showindex: - assert result == yes_index_result + expected = yes_index_result else: - assert result == no_index_result + expected = no_index_result else: if index in [True, None]: - assert result == yes_index_result + expected = yes_index_result else: - assert result == no_index_result + expected = no_index_result + assert result == expected From bc2d998290c3d27c377dd59e02ebcca787988e93 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Tue, 7 Jul 2020 19:49:40 +0700 Subject: [PATCH 15/18] remove redundant code --- pandas/core/frame.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7660f313ba9e9..ef03d264ff329 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -2224,9 +2224,6 @@ def to_markdown( **kwargs, ) -> Optional[str]: if "showindex" in kwargs: - index = kwargs["showindex"] - del kwargs["showindex"] - warnings.warn( "'showindex' is deprecated. Only 'index' will be used " "in a future version. Use 'index' to silence this warning.", From c822daf1a3caeb71e007957ce8539a524a9a1d4e Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 8 Jul 2020 09:53:17 +0700 Subject: [PATCH 16/18] update docstring --- pandas/core/series.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 53d93d24ecd21..43bf270528080 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1436,6 +1436,8 @@ def to_markdown( Buffer to write to. If None, the output is returned as a string. mode : str, optional Mode in which file is opened. + index : bool, optional, default True + Add index (row) labels **kwargs These parameters will be passed to `tabulate \ `_. From 2085ed5f906aa130be97a8e512601633b26cf8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quang=20Nguy=E1=BB=85n?= <30631476+quangngd@users.noreply.github.com> Date: Wed, 8 Jul 2020 20:09:00 +0700 Subject: [PATCH 17/18] Update pandas/core/series.py Co-authored-by: Simon Hawkins --- pandas/core/series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 43bf270528080..355807a4b3e9a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1437,7 +1437,7 @@ def to_markdown( mode : str, optional Mode in which file is opened. index : bool, optional, default True - Add index (row) labels + Add index (row) labels. **kwargs These parameters will be passed to `tabulate \ `_. From 71a9b6bf750bd4a7c9fd05f1e2e4300a490ba49b Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Wed, 15 Jul 2020 09:57:43 +0700 Subject: [PATCH 18/18] add versionadded --- pandas/core/series.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 43bf270528080..b13702fb8b728 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1438,6 +1438,9 @@ def to_markdown( Mode in which file is opened. index : bool, optional, default True Add index (row) labels + + .. versionadded:: 1.1.0 + **kwargs These parameters will be passed to `tabulate \ `_.