Skip to content

Conversation

@fserucas
Copy link
Contributor

This change adds a imputeExpr function which behaves like impute but accepts Col Expr. #45

This change adds a `imputeExpr` function which behaves like `impute`
but accepts Col Expr.
@fserucas
Copy link
Contributor Author

It was a challenge understanding this, and i am still not sure about this.
Could you guide me.
Tks

Nothing -> throw $ ColumnNotFoundException columnName "impute" (map fst $ M.toList $ columnIndices df)
Just (OptionalColumn _) ->
case expr of
Col a -> impute columnName a df
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Col a is just a typed reference to a column. It sort of looks like: data Expr = Col String | .... As written, a === columnName. You probably want to interpret the expression, get the resulting column then zip it with the result of getColumn.

@adityakaldate21-dev
Copy link

-- src/DataFrame/Operations/Transformations.hs

{-# LANGUAGE OverloadedStrings #-}

module DataFrame.Operations.Transformations (imputeExpr) where

import qualified Data.Map as M
import DataFrame.Types
import DataFrame.Exceptions
import DataFrame.Utils (getColumn, setColumn, zipWithDefault)

-- Simplified expression type
data Expr = Col String | Lit Value

-- Evaluate an expression to a column of values
evalExpr :: Expr -> DataFrame -> [Value]
evalExpr (Col name) df =
case M.lookup name (columnIndices df) of
Just _ -> getColumn name df
Nothing -> error $ "Column " ++ name ++ " not found in expression"
evalExpr (Lit v) _ = repeat v

-- Impute missing values in a column using values from an expression
imputeExpr :: ColumnName -> Expr -> DataFrame -> DataFrame
imputeExpr columnName expr df =
case M.lookup columnName (columnIndices df) of
Nothing -> throw $ ColumnNotFoundException columnName "imputeExpr" (map fst $ M.toList $ columnIndices df)
Just (OptionalColumn _) ->
let targetCol = getColumn columnName df
sourceCol = evalExpr expr df
imputedCol = zipWithDefault targetCol sourceCol
in setColumn columnName imputedCol df
Just _ -> throw $ InvalidColumnTypeException columnName "Expected OptionalColumn"

-- Replace Nothing in targetCol with corresponding value from sourceCol
zipWithDefault :: [Value] -> [Value] -> [Value]
zipWithDefault = zipWith (\t s -> case t of { Nothing -> s; Just _ -> t })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants