Skip to content

Commit 48289b5

Browse files
committed
👌 return string if buf is set to None, use shared doc, change version introduced in to 1.0.0
1 parent 474a354 commit 48289b5

File tree

4 files changed

+62
-45
lines changed

4 files changed

+62
-45
lines changed

pandas/core/frame.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -1966,31 +1966,30 @@ def to_feather(self, path):
19661966

19671967
to_feather(self, path)
19681968

1969-
def to_markdown(
1970-
self,
1971-
buf: Optional[FilePathOrBuffer[str]] = None,
1972-
mode: Optional[str] = None,
1973-
**kwargs,
1974-
) -> None:
1969+
@Appender(
19751970
"""
1976-
Print a DataFrame in Markdown-friendly format.
1977-
1978-
.. versionadded:: 1.0
1979-
1980-
Returns
1981-
-------
1982-
str
1983-
DataFrame in Markdown-friendly format.
1984-
19851971
Examples
19861972
--------
1987-
>>> df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
1973+
>>> df = pd.DataFrame(
1974+
... data={"animal_1": ["elk", "pig"], "animal_2": ["dog", "quetzal"]}
1975+
... )
19881976
>>> print(df.to_markdown())
1989-
| | col1 | col2 |
1990-
|---:|-------:|-------:|
1991-
| 0 | 1 | 3 |
1992-
| 1 | 2 | 4 |
1977+
| | animal_1 | animal_2 |
1978+
|---:|:-----------|:-----------|
1979+
| 0 | elk | dog |
1980+
| 1 | pig | quetzal |
19931981
"""
1982+
)
1983+
@Substitution(klass="DataFrame")
1984+
@Appender(_shared_docs["to_markdown"])
1985+
def to_markdown(
1986+
self,
1987+
buf: Optional[FilePathOrBuffer[str]] = None,
1988+
mode: Optional[str] = None,
1989+
**kwargs,
1990+
) -> Optional[str]:
1991+
kwargs.setdefault("headers", "keys")
1992+
kwargs.setdefault("tablefmt", "pipe")
19941993
tabulate = import_optional_dependency("tabulate")
19951994
result = tabulate.tabulate(self, **kwargs)
19961995
if buf is None:

pandas/core/generic.py

+21
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,27 @@ def _repr_data_resource_(self):
20092009
# ----------------------------------------------------------------------
20102010
# I/O Methods
20112011

2012+
_shared_docs[
2013+
"to_markdown"
2014+
] = """
2015+
Print %(klass)s in Markdown-friendly format.
2016+
2017+
.. versionadded:: 1.0.0
2018+
2019+
Parameters
2020+
----------
2021+
buf : writable buffer, defaults to sys.stdout
2022+
Where to send the output. By default, the output is printed to
2023+
sys.stdout. Pass a writable buffer if you need to further process
2024+
the output.
2025+
mode : str, optional
2026+
2027+
Returns
2028+
-------
2029+
str
2030+
%(klass)s in Markdown-friendly format.
2031+
"""
2032+
20122033
_shared_docs[
20132034
"to_excel"
20142035
] = """

pandas/core/series.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import warnings
99

1010
import numpy as np
11+
from pandas.core.generic import _shared_docs
1112

1213
from pandas._config import get_option
1314

@@ -1435,33 +1436,28 @@ def to_string(
14351436
with open(buf, "w") as f:
14361437
f.write(result)
14371438

1438-
def to_markdown(
1439-
self,
1440-
buf: Optional[FilePathOrBuffer[str]] = None,
1441-
mode: Optional[str] = None,
1442-
**kwargs,
1443-
) -> str:
1439+
@Appender(
14441440
"""
1445-
Print a Series in Markdown-friendly format.
1446-
1447-
.. versionadded:: 1.0
1448-
1449-
Returns
1450-
-------
1451-
str
1452-
Series in Markdown-friendly format.
1453-
14541441
Examples
14551442
--------
1456-
>>> s = pd.Series([1, 2, 3, 4])
1443+
>>> s = pd.Series(["elk", "pig", "dog", "quetzal"], name="animal")
14571444
>>> print(s.to_markdown())
1458-
| | 0 |
1459-
|---:|----:|
1460-
| 0 | 1 |
1461-
| 1 | 2 |
1462-
| 2 | 3 |
1463-
| 3 | 4 |
1445+
| | animal |
1446+
|---:|:---------|
1447+
| 0 | elk |
1448+
| 1 | pig |
1449+
| 2 | dog |
1450+
| 3 | quetzal |
14641451
"""
1452+
)
1453+
@Substitution(klass="Series")
1454+
@Appender(_shared_docs["to_markdown"])
1455+
def to_markdown(
1456+
self,
1457+
buf: Optional[FilePathOrBuffer[str]] = None,
1458+
mode: Optional[str] = None,
1459+
**kwargs,
1460+
) -> Optional[str]:
14651461
return self.to_frame().to_markdown(buf, mode, **kwargs)
14661462

14671463
# ----------------------------------------------------------------------

pandas/tests/io/formats/test_to_markdown.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def test_series():
4949

5050
def test_no_buf(capsys):
5151
df = pd.DataFrame([1, 2, 3])
52-
df.to_markdown()
53-
out, _ = capsys.readouterr()
54-
assert out == "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
52+
result = df.to_markdown()
53+
assert (
54+
result == "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
55+
)

0 commit comments

Comments
 (0)