|
15 | 15 | import sys
|
16 | 16 | from textwrap import dedent
|
17 | 17 | from typing import (
|
| 18 | + IO, |
18 | 19 | Any,
|
19 | 20 | FrozenSet,
|
20 | 21 | Hashable,
|
|
37 | 38 |
|
38 | 39 | from pandas._libs import algos as libalgos, lib
|
39 | 40 | from pandas._typing import Axes, Dtype, FilePathOrBuffer
|
| 41 | +from pandas.compat._optional import import_optional_dependency |
40 | 42 | from pandas.compat.numpy import function as nv
|
41 | 43 | from pandas.util._decorators import (
|
42 | 44 | Appender,
|
|
118 | 120 | from pandas.core.ops.missing import dispatch_fill_zeros
|
119 | 121 | from pandas.core.series import Series
|
120 | 122 |
|
| 123 | +from pandas.io.common import get_filepath_or_buffer |
121 | 124 | from pandas.io.formats import console, format as fmt
|
122 | 125 | from pandas.io.formats.printing import pprint_thing
|
123 | 126 | import pandas.plotting
|
@@ -1964,6 +1967,36 @@ def to_feather(self, path):
|
1964 | 1967 |
|
1965 | 1968 | to_feather(self, path)
|
1966 | 1969 |
|
| 1970 | + @Appender( |
| 1971 | + """ |
| 1972 | + Examples |
| 1973 | + -------- |
| 1974 | + >>> df = pd.DataFrame( |
| 1975 | + ... data={"animal_1": ["elk", "pig"], "animal_2": ["dog", "quetzal"]} |
| 1976 | + ... ) |
| 1977 | + >>> print(df.to_markdown()) |
| 1978 | + | | animal_1 | animal_2 | |
| 1979 | + |---:|:-----------|:-----------| |
| 1980 | + | 0 | elk | dog | |
| 1981 | + | 1 | pig | quetzal | |
| 1982 | + """ |
| 1983 | + ) |
| 1984 | + @Substitution(klass="DataFrame") |
| 1985 | + @Appender(_shared_docs["to_markdown"]) |
| 1986 | + def to_markdown( |
| 1987 | + self, buf: Optional[IO[str]] = None, mode: Optional[str] = None, **kwargs, |
| 1988 | + ) -> Optional[str]: |
| 1989 | + kwargs.setdefault("headers", "keys") |
| 1990 | + kwargs.setdefault("tablefmt", "pipe") |
| 1991 | + tabulate = import_optional_dependency("tabulate") |
| 1992 | + result = tabulate.tabulate(self, **kwargs) |
| 1993 | + if buf is None: |
| 1994 | + return result |
| 1995 | + buf, _, _, _ = get_filepath_or_buffer(buf, mode=mode) |
| 1996 | + assert buf is not None # Help mypy. |
| 1997 | + buf.writelines(result) |
| 1998 | + return None |
| 1999 | + |
1967 | 2000 | @deprecate_kwarg(old_arg_name="fname", new_arg_name="path")
|
1968 | 2001 | def to_parquet(
|
1969 | 2002 | self,
|
|
0 commit comments