Skip to content

Commit fd0c1c8

Browse files
committed
Add types to output table when printing dataframe
1 parent f1413d1 commit fd0c1c8

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/Data/DataFrame/Internal.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ instance Show DataFrame where
105105
asText :: DataFrame -> T.Text
106106
asText d =
107107
let header = "index" : map fst (sortBy (compare `on` snd) $ M.toList (columnIndices d))
108+
types = V.toList $ V.filter (/= "") $ V.map getType (columns d)
109+
getType Nothing = ""
110+
getType (Just (BoxedColumn (column :: Vector a))) = T.pack $ show (Type.Reflection.typeRep @a)
111+
getType (Just (UnboxedColumn (column :: VU.Vector a))) = T.pack $ show (Type.Reflection.typeRep @a)
108112
-- Separate out cases dynamically so we don't end up making round trip string
109113
-- copies.
110114
get (Just (BoxedColumn (column :: Vector a))) =
@@ -123,7 +127,7 @@ asText d =
123127
rows =
124128
transpose $
125129
zipWith (curry (V.toList . getTextColumnFromFrame d)) [0..] header
126-
in showTable header rows
130+
in showTable header ("Int":types) rows
127131

128132
metadata :: DataFrame -> String
129133
metadata df = show (columnIndices df) ++ "\n" ++

src/Data/DataFrame/Util.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ right = fillRight ' '
6363
center :: Int -> T.Text -> T.Text
6464
center = fillCenter ' '
6565

66-
showTable :: [T.Text] -> [[T.Text]] -> T.Text
67-
showTable header rows =
66+
showTable :: [T.Text] -> [T.Text] -> [[T.Text]] -> T.Text
67+
showTable header types rows =
6868
let cs = map (\h -> ColDesc center h left) header
69-
widths = [maximum $ map T.length col | col <- transpose $ header : rows]
69+
widths = [maximum $ map T.length col | col <- transpose $ header : types : rows]
70+
border = T.intercalate "---" [T.replicate width (T.singleton '-') | width <- widths]
7071
separator = T.intercalate "-|-" [T.replicate width (T.singleton '-') | width <- widths]
7172
fillCols fill cols = T.intercalate " | " [fill c width col | (c, width, col) <- zip3 cs widths cols]
72-
in T.unlines $ fillCols colTitleFill header : separator : map (fillCols colValueFill) rows
73+
in T.unlines $ border : fillCols colTitleFill header : separator : fillCols colTitleFill types : separator : map (fillCols colValueFill) rows
7374

7475
headOr :: a -> [a] -> a
7576
headOr v [] = v

0 commit comments

Comments
 (0)