44{-# LANGUAGE ScopedTypeVariables #-}
55{-# LANGUAGE TypeApplications #-}
66{-# LANGUAGE GADTs #-}
7- {-# LANGUAGE Strict #-}
87{-# LANGUAGE FlexibleContexts #-}
98module DataFrame.Internal.DataFrame where
109
@@ -25,11 +24,9 @@ import Type.Reflection (typeRep)
2524data DataFrame = DataFrame
2625 { -- | Our main data structure stores a dataframe as
2726 -- a vector of columns. This improv
28- columns :: V. Vector ( Maybe Column ) ,
27+ columns :: V. Vector Column ,
2928 -- | Keeps the column names in the order they were inserted in.
3029 columnIndices :: M. Map T. Text Int ,
31- -- | Next free index that we insert a column into.
32- freeIndices :: [Int ],
3330 dataframeDimensions :: (Int , Int )
3431 }
3532
@@ -46,13 +43,12 @@ asText :: DataFrame -> Bool -> T.Text
4643asText d properMarkdown =
4744 let header = " index" : map fst (sortBy (compare `on` snd ) $ M. toList (columnIndices d))
4845 types = V. toList $ V. filter (/= " " ) $ V. map getType (columns d)
49- getType :: Maybe Column -> T. Text
50- getType Nothing = " "
51- getType (Just (BoxedColumn (column :: V. Vector a ))) = T. pack $ show (typeRep @ a )
52- getType (Just (UnboxedColumn (column :: VU. Vector a ))) = T. pack $ show (typeRep @ a )
53- getType (Just (OptionalColumn (column :: V. Vector a ))) = T. pack $ show (typeRep @ a )
54- getType (Just (GroupedBoxedColumn (column :: V. Vector a ))) = T. pack $ show (typeRep @ a )
55- getType (Just (GroupedUnboxedColumn (column :: V. Vector a ))) = T. pack $ show (typeRep @ a )
46+ getType :: Column -> T. Text
47+ getType (BoxedColumn (column :: V. Vector a )) = T. pack $ show (typeRep @ a )
48+ getType (UnboxedColumn (column :: VU. Vector a )) = T. pack $ show (typeRep @ a )
49+ getType (OptionalColumn (column :: V. Vector a )) = T. pack $ show (typeRep @ a )
50+ getType (GroupedBoxedColumn (column :: V. Vector a )) = T. pack $ show (typeRep @ a )
51+ getType (GroupedUnboxedColumn (column :: V. Vector a )) = T. pack $ show (typeRep @ a )
5652 -- Separate out cases dynamically so we don't end up making round trip string
5753 -- copies.
5854 get :: Maybe Column -> V. Vector T. Text
@@ -67,32 +63,22 @@ asText d properMarkdown =
6763 get (Just (GroupedUnboxedColumn column)) = V. map (T. pack . show ) column
6864 getTextColumnFromFrame df (i, name) = if i == 0
6965 then V. fromList (map (T. pack . show ) [0 .. (fst (dataframeDimensions df) - 1 )])
70- else get $ (V. !) (columns d) ((M. !) (columnIndices d) name)
66+ else get $ (V. !? ) (columns d) ((M. !) (columnIndices d) name)
7167 rows =
7268 transpose $
7369 zipWith (curry (V. toList . getTextColumnFromFrame d)) [0 .. ] header
7470 in (if properMarkdown then showTableProperMarkdown else showTable) header (" Int" : types) rows
7571
7672-- | O(1) Creates an empty dataframe
7773empty :: DataFrame
78- empty = DataFrame {columns = V. replicate initialColumnSize Nothing ,
74+ empty = DataFrame {columns = V. empty ,
7975 columnIndices = M. empty,
80- freeIndices = [0 .. (initialColumnSize - 1 )],
8176 dataframeDimensions = (0 , 0 ) }
8277
83- initialColumnSize :: Int
84- initialColumnSize = 8
85-
8678getColumn :: T. Text -> DataFrame -> Maybe Column
8779getColumn name df = do
8880 i <- columnIndices df M. !? name
89- join $ columns df V. !? i
81+ columns df V. !? i
9082
9183null :: DataFrame -> Bool
92- null df = dataframeDimensions df == (0 , 0 )
93-
94- metadata :: DataFrame -> String
95- metadata df = show (columnIndices df) ++ " \n " ++
96- show (V. map (fmap columnVersionString) (columns df)) ++ " \n " ++
97- show (freeIndices df) ++ " \n " ++
98- show (dataframeDimensions df)
84+ null df = V. null (columns df)
0 commit comments