Skip to content

ENH: Add index option to_markdown() #33091

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 30 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5bfdc21
add index kw and tests
quangngd Mar 28, 2020
d74be05
add for series, edit docs
quangngd Mar 28, 2020
decac71
add comment and doc
quangngd Mar 30, 2020
12b96a2
change wanings to error
quangngd Mar 30, 2020
ff0b970
completely remove showindex kw
quangngd Apr 6, 2020
19f4219
Merge branch 'master' into to_markdown-index
quangngd Apr 6, 2020
334179e
Merge branch 'master' into to_markdown-index
quangngd May 9, 2020
2f697f1
Merge branch 'to_markdown-index' of https://github.com/quangngd/panda…
quangngd May 9, 2020
e519fa3
Update v1.1.0.rst
quangngd May 9, 2020
8cc92c1
Update v1.1.0.rst
quangngd May 9, 2020
9f1b0c8
Merge branch 'master' into to_markdown-index
quangngd May 11, 2020
1684129
Merge branch 'master' into to_markdown-index
quangngd May 26, 2020
492f72f
change ValueError to FutureWarning
quangngd Jun 18, 2020
84d965e
Merge branch 'master' into to_markdown-index
quangngd Jun 18, 2020
04e25df
Merge branch 'master' into to_markdown-index
quangngd Jun 18, 2020
d54d6c8
Merge branch 'to_markdown-index' of https://github.com/quangngd/panda…
quangngd Jun 18, 2020
c520f83
Merge branch 'master' into to_markdown-index
quangngd Jul 1, 2020
5670f67
fix warnings
quangngd Jul 1, 2020
e80230a
fix import
quangngd Jul 1, 2020
992f923
refactor test, fix typing and doc
quangngd Jul 2, 2020
68cf1f7
fix blacks split line
quangngd Jul 2, 2020
9ee640f
refactor test
quangngd Jul 7, 2020
bfa72d8
Merge branch 'master' into to_markdown-index
quangngd Jul 7, 2020
82bb822
refactor test
quangngd Jul 7, 2020
bc2d998
remove redundant code
quangngd Jul 7, 2020
c822daf
update docstring
quangngd Jul 8, 2020
2085ed5
Update pandas/core/series.py
quangngd Jul 8, 2020
71a9b6b
add versionadded
quangngd Jul 15, 2020
be6ad87
Merge branch 'master' into to_markdown-index
quangngd Jul 15, 2020
227a7e1
Merge branch 'to_markdown-index' of https://github.com/quangngd/panda…
quangngd Jul 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +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` and :meth:`Series.to_markdown` now accept ``index`` argument as an alias for tabulate's ``showindex`` (:issue:`32667`)
- :meth:`read_csv` now accepts string values like "0", "0.0", "1", "1.0" as convertible to the nullable boolean dtype (:issue:`34859`)
- :class:`pandas.core.window.ExponentialMovingWindow` now supports a ``times`` argument that allows ``mean`` to be calculated with observations spaced by the timestamps in ``times`` (:issue:`34839`)

Expand Down
15 changes: 14 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2217,10 +2217,23 @@ def to_feather(self, path, **kwargs) -> None:
""",
)
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: bool = True,
**kwargs,
) -> Optional[str]:
if "showindex" in kwargs:
warnings.warn(
"'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")
kwargs.setdefault("tablefmt", "pipe")
kwargs.setdefault("showindex", index)
tabulate = import_optional_dependency("tabulate")
result = tabulate.tabulate(self, **kwargs)
if buf is None:
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,11 @@ def to_string(
),
)
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: bool = True,
**kwargs,
) -> Optional[str]:
"""
Print {klass} in Markdown-friendly format.
Expand All @@ -1432,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 \
<https://pypi.org/project/tabulate>`_.
Expand Down Expand Up @@ -1467,7 +1473,7 @@ def to_markdown(
| 3 | quetzal |
+----+----------+
"""
return self.to_frame().to_markdown(buf, mode, **kwargs)
return self.to_frame().to_markdown(buf, mode, index, **kwargs)

# ----------------------------------------------------------------------

Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/io/formats/test_to_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import pandas as pd
import pandas._testing as tm

pytest.importorskip("tabulate")

Expand Down Expand Up @@ -53,3 +54,37 @@ 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):
# GH 32667
kwargs = {}
if index is not None:
kwargs["index"] = index
if showindex is not None:
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 |"

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:
expected = yes_index_result
else:
expected = no_index_result
else:
if index in [True, None]:
expected = yes_index_result
else:
expected = no_index_result
assert result == expected