Skip to content

Commit d27ffb3

Browse files
author
Marco Gorelli
committed
✨ add to_markdown to Series
1 parent b34cadb commit d27ffb3

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pandas/core/series.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,28 @@ def to_string(
14341434
with open(buf, "w") as f:
14351435
f.write(result)
14361436

1437+
def to_markdown(self, **kwargs):
1438+
"""
1439+
Print a Series in markdown-friendly format.
1440+
1441+
.. versionadded:: 1.0
1442+
1443+
Returns
1444+
-------
1445+
str
1446+
Series in markdown-friendly format.
1447+
1448+
Examples
1449+
--------
1450+
>>> s = pd.Series([1, 2, 3, 4])
1451+
>>> print(s.to_markdown())
1452+
| | col1 | col2 |
1453+
|---:|-------:|-------:|
1454+
| 0 | 1 | 3 |
1455+
| 1 | 2 | 4 |
1456+
"""
1457+
return self.to_frame().to_markdown(**kwargs)
1458+
14371459
# ----------------------------------------------------------------------
14381460

14391461
def items(self):

pandas/tests/io/formats/test_to_markdown.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,31 @@
55

66
@td.skip_if_no_tabulate
77
class TestToMarkdown:
8-
def test_to_markdown(self):
8+
def test_simple(self):
99
df = pd.DataFrame([1, 2, 3])
1010
result = df.to_markdown()
1111
assert (
1212
result
1313
== "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
1414
)
1515

16-
def test_to_markdown_other_tablefmt(self):
16+
def test_other_tablefmt(self):
1717
df = pd.DataFrame([1, 2, 3])
1818
result = df.to_markdown(tablefmt="jira")
1919
assert result == "|| || 0 ||\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
20+
21+
def test_other_headers(self):
22+
df = pd.DataFrame([1, 2, 3])
23+
result = df.to_markdown(headers=["foo", "bar"])
24+
assert (
25+
result
26+
== "| foo | bar |\n|------:|------:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
27+
)
28+
29+
def test_series(self):
30+
s = pd.Series([1, 2, 3], name="foo")
31+
result = s.to_markdown()
32+
assert (
33+
result
34+
== "| | foo |\n|---:|------:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
35+
)

0 commit comments

Comments
 (0)