Skip to content

Commit

Permalink
add df pretty formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Oct 4, 2018
1 parent a463fac commit c93d4f0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.0.5 (c2dbde08d26fdfb2f2eefdea12d497b802815362 at 10:46:11 18-10-04 +0200)
10.0.6 (2bb921a5012b9a923b68e7b7d1a9f8afb50957f5 at 12:30:47 18-10-04 +0200)
18 changes: 18 additions & 0 deletions hal/data/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,21 @@ def decode(self, lb):
lb.inverse_transform(row)
for row in self.matrix
]

@staticmethod
def from_columns(columns):
"""
:param columns: [] of [] of anything
Matrix divided into columns
:return: Matrix
Merge the columns to form a matrix
"""

data = [
[
column[i]
for i in range(len(column))
]
for column in columns
]
return Matrix(data)
29 changes: 27 additions & 2 deletions hal/streams/pretty_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def parse_colorama(text):
return non_ansi_string(text)


class PrettyTable:
class SqlTable:
def __init__(self, labels, data, line_separator):
"""
:param labels: [] of str
Expand Down Expand Up @@ -128,6 +128,19 @@ def build(self):

return pretty_table

@staticmethod
def from_df(df):
"""
:param df: pandas.DataFrame
Data
:return: SqlTable
Parses data and builds an instance of this class
"""

labels = df.columns.tolist()
data = df.reset_index().values.tolist()
return SqlTable(labels, data, "\n")


def pretty_format_table(labels, data, line_separator="\n"):
"""
Expand All @@ -141,5 +154,17 @@ def pretty_format_table(labels, data, line_separator="\n"):
Pretty formatted table (first row is labels, then actual data)
"""

table = PrettyTable(labels, data, line_separator)
table = SqlTable(labels, data, line_separator)
return table.build()


def pretty_df(df):
"""
:param df: pandas.DataFrame
Data
:return: str
Pretty formatted table (first row is labels, then actual data)
"""

table = SqlTable.from_df(df)
return table.build()

0 comments on commit c93d4f0

Please sign in to comment.