Feature Type

  • [X] Adding new functionality to pandas

  • [ ] Changing existing functionality in pandas

  • [ ] Removing existing functionality in pandas

Problem Description

I wish I could use ReStructured Text with pandas

Feature Description

The end users code:

import pandas as pd
df=pd.read_rst(rst)
df.to_rst()

I believe tabulate has a way to do this.

Alternative Solutions

I also built a way to make rst tables.

Additional Context

I think Grid Tables would be best for pandas (or Simple Tables)

I did not use sudo-code in the examples due to complexity and that examples of how to do this can be seen in the above packages. See RST docs for what they look like.

Comment From: haiyashah

Assign

Comment From: haiyashah

import pandas as pd from tabulate import tabulate from docutils.parsers.rst import tableparser from docutils.statemachine import StringList

def read_rst(rst_string): parser = tableparser.GridTableParser() table_data = parser.parse(StringList(rst_string.splitlines())) header = ["".join(cell) for cell in table_data[0]] rows = [[cell[0] for cell in row] for row in table_data[1]] return pd.DataFrame(rows, columns=header)

def to_rst(df): return tabulate(df, headers="keys", tablefmt="grid")

rst_table = """ +-----+-------+ | ID | Name | +=====+=======+ | 1 | Alice | +-----+-------+ | 2 | Bob | +-----+-------+ """

df = read_rst(rst_table) print(df)

rst_output = to_rst(df) print(rst_output)

Comment From: R5dan

I'm not saying its not possible to transform them, but its not as simple as it could be. It should be able to be done inside pandas

rst_table = """ +-----+-------+ | ID | Name | +=====+=======+ | 1 | Alice | +-----+-------+ | 2 | Bob | +-----+-------+ """

Thats an invalid RST table, so shouldn't be parsable.

Comment From: R5dan

Does anyone else have any suggestions?