dataframe-operations 1.1.1.0 → 1.1.1.1
raw patch · 3 files changed
+38/−22 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ DataFrame.Monad: columnAsListM :: Columnable a => Expr a -> FrameM [a]
+ DataFrame.Monad: dropM :: Int -> FrameM ()
+ DataFrame.Monad: execWithExpr :: Columnable a => Text -> (DataFrame -> DataFrame) -> FrameM (Expr a)
+ DataFrame.Monad: insertM :: Columnable a => Text -> [a] -> FrameM (Expr a)
+ DataFrame.Operations.Core: withColumnName :: Text -> Either DataFrameException a -> Either DataFrameException a
Files
- dataframe-operations.cabal +2/−2
- src/DataFrame/Monad.hs +21/−15
- src/DataFrame/Operations/Core.hs +15/−5
dataframe-operations.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: dataframe-operations-version: 1.1.1.0+version: 1.1.1.1 synopsis: Column operations, expression DSL, and statistics for the dataframe ecosystem. description: Untyped column operations (select, filter, sort, join, groupBy,@@ -58,7 +58,7 @@ dataframe-parsing ^>= 1.0.2, random >= 1.2 && < 2, regex-tdfa >= 1.3.0 && < 2,- text >= 2.0 && < 3,+ text >= 2.1 && < 3, time >= 1.12 && < 2, vector ^>= 0.13, vector-algorithms ^>= 0.9
src/DataFrame/Monad.hs view
@@ -18,6 +18,7 @@ import qualified DataFrame.Operations.Transformations as D import qualified Data.Text as T+import Data.Tuple (swap) import System.Random -- A re-implementation of the state monad.@@ -52,15 +53,20 @@ inspectM :: (DataFrame -> b) -> FrameM b inspectM f = FrameM $ \df -> (df, f df) -deriveM :: (Columnable a) => T.Text -> Expr a -> FrameM (Expr a)-deriveM name expr = FrameM $ \df ->- let df' = D.derive name expr df+execWithExpr ::+ (Columnable a) => T.Text -> (DataFrame -> DataFrame) -> FrameM (Expr a)+execWithExpr name f = FrameM $ \df ->+ let df' = f df in (df', Col name) +deriveM :: (Columnable a) => T.Text -> Expr a -> FrameM (Expr a)+deriveM name expr = execWithExpr name (D.derive name expr)++insertM :: (Columnable a) => T.Text -> [a] -> FrameM (Expr a)+insertM name values = execWithExpr name (D.insert name values)+ renameM :: (Columnable a) => Expr a -> T.Text -> FrameM (Expr a)-renameM (Col oldName) newName = FrameM $ \df ->- let df' = D.rename oldName newName df- in (df', Col newName)+renameM (Col oldName) newName = execWithExpr newName (D.rename oldName newName) renameM expr newName = deriveM newName expr filterWhereM :: Expr Bool -> FrameM ()@@ -72,10 +78,14 @@ takeM :: Int -> FrameM () takeM n = modifyM (D.take n) +dropM :: Int -> FrameM ()+dropM n = modifyM (D.drop n)++columnAsListM :: (Columnable a) => Expr a -> FrameM [a]+columnAsListM c = inspectM (D.columnAsList c)+ filterJustM :: (Columnable a) => Expr (Maybe a) -> FrameM (Expr a)-filterJustM (Col name) = FrameM $ \df ->- let df' = D.filterJust name df- in (df', Col name)+filterJustM (Col name) = execWithExpr name (D.filterJust name) filterJustM expr = error $ "Cannot filter on compound expression: " ++ show expr @@ -84,15 +94,11 @@ Expr a -> BaseType a -> FrameM (Expr (BaseType a))-imputeM expr@(Col name) value = FrameM $ \df ->- let df' = D.impute expr value df- in (df', Col name)+imputeM expr@(Col name) value = execWithExpr name (D.impute expr value) imputeM expr _ = error $ "Cannot impute on compound expression: " ++ show expr runFrameM :: DataFrame -> FrameM a -> (a, DataFrame)-runFrameM df (FrameM action) =- let (df', a) = action df- in (a, df')+runFrameM df (FrameM action) = swap (action df) evalFrameM :: DataFrame -> FrameM a -> a evalFrameM df m = fst (runFrameM df m)
src/DataFrame/Operations/Core.hs view
@@ -796,6 +796,16 @@ (fst (dataframeDimensions df)) (\i -> VU.generate (V.length m) (\j -> (m VG.! j) VG.! i)) +withColumnName ::+ T.Text -> Either DataFrameException a -> Either DataFrameException a+withColumnName name (Left (TypeMismatchException ctx)) =+ Left+ (TypeMismatchException ctx{errorColumnName = orFallback (errorColumnName ctx)})+ where+ orFallback (Just n) = Just n+ orFallback Nothing = Just (T.unpack name)+withColumnName _ result = result+ {- | Get a specific column as a vector. You must specify the type via type applications.@@ -815,7 +825,7 @@ | null df = throw (EmptyDataSetException "columnAsVector") | otherwise = case expr of (Col name) -> case getColumn name df of- Just col -> toVector col+ Just col -> withColumnName name (toVector col) Nothing -> Left $ ColumnsNotFoundException [name] "columnAsVector" (M.keys $ columnIndices df)@@ -832,7 +842,7 @@ (Columnable a, Num a) => Expr a -> DataFrame -> Either DataFrameException (VU.Vector Int) columnAsIntVector (Col name) df = case getColumn name df of- Just col -> toIntVector col+ Just col -> withColumnName name (toIntVector col) Nothing -> Left $ ColumnsNotFoundException [name] "columnAsIntVector" (M.keys $ columnIndices df)@@ -849,7 +859,7 @@ (Columnable a, Num a) => Expr a -> DataFrame -> Either DataFrameException (VU.Vector Double) columnAsDoubleVector (Col name) df = case getColumn name df of- Just col -> toDoubleVector col+ Just col -> withColumnName name (toDoubleVector col) Nothing -> Left $ ColumnsNotFoundException@@ -869,7 +879,7 @@ (Columnable a, Num a) => Expr a -> DataFrame -> Either DataFrameException (VU.Vector Float) columnAsFloatVector (Col name) df = case getColumn name df of- Just col -> toFloatVector col+ Just col -> withColumnName name (toFloatVector col) Nothing -> Left $ ColumnsNotFoundException@@ -885,7 +895,7 @@ (Columnable a, VU.Unbox a) => Expr a -> DataFrame -> Either DataFrameException (VU.Vector a) columnAsUnboxedVector (Col name) df = case getColumn name df of- Just col -> toUnboxedVector col+ Just col -> withColumnName name (toUnboxedVector col) Nothing -> Left $ ColumnsNotFoundException