File tree 2 files changed +40
-2
lines changed
2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -1434,6 +1434,28 @@ def to_string(
1434
1434
with open (buf , "w" ) as f :
1435
1435
f .write (result )
1436
1436
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
+
1437
1459
# ----------------------------------------------------------------------
1438
1460
1439
1461
def items (self ):
Original file line number Diff line number Diff line change 5
5
6
6
@td .skip_if_no_tabulate
7
7
class TestToMarkdown :
8
- def test_to_markdown (self ):
8
+ def test_simple (self ):
9
9
df = pd .DataFrame ([1 , 2 , 3 ])
10
10
result = df .to_markdown ()
11
11
assert (
12
12
result
13
13
== "| | 0 |\n |---:|----:|\n | 0 | 1 |\n | 1 | 2 |\n | 2 | 3 |"
14
14
)
15
15
16
- def test_to_markdown_other_tablefmt (self ):
16
+ def test_other_tablefmt (self ):
17
17
df = pd .DataFrame ([1 , 2 , 3 ])
18
18
result = df .to_markdown (tablefmt = "jira" )
19
19
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
+ )
You can’t perform that action at this time.
0 commit comments