Skip to content

Commit 23b181f

Browse files
committed
feat: Utilities to get column as vector or matrix
1 parent d79f54d commit 23b181f

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/DataFrame/Internal/DataFrame.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import qualified Data.Map as M
1111
import qualified Data.Text as T
1212
import qualified Data.Vector as V
1313
import qualified Data.Vector.Unboxed as VU
14+
import qualified Data.Vector.Generic as VG
1415

1516
import Control.Monad (join)
1617
import DataFrame.Display.Terminal.PrettyPrint
@@ -98,3 +99,20 @@ unsafeGetColumn name df = columns df V.! (columnIndices df M.! name)
9899

99100
null :: DataFrame -> Bool
100101
null df = V.null (columns df)
102+
103+
toMatrix :: DataFrame -> V.Vector (VU.Vector Float)
104+
toMatrix df = let
105+
m = V.map (toVector @Double) (columns df)
106+
in V.generate (fst (dataframeDimensions df)) (\i -> foldl (\acc j -> acc `VU.snoc` (realToFrac ((m V.! j) V.! i))) VU.empty [0..(V.length m - 1)])
107+
108+
columnAsVector :: forall a . Columnable a => T.Text -> DataFrame -> V.Vector a
109+
columnAsVector name df = case unsafeGetColumn name df of
110+
(BoxedColumn (col :: V.Vector b)) -> case testEquality (typeRep @a) (typeRep @b) of
111+
Nothing -> error "Type error"
112+
Just Refl -> col
113+
(OptionalColumn (col :: V.Vector b)) -> case testEquality (typeRep @a) (typeRep @b) of
114+
Nothing -> error "Type error"
115+
Just Refl -> col
116+
(UnboxedColumn (col :: VU.Vector b)) -> case testEquality (typeRep @a) (typeRep @b) of
117+
Nothing -> error "Type error"
118+
Just Refl -> VG.convert col

0 commit comments

Comments
 (0)