Skip to content

Commit 4ed1e0e

Browse files
committed
feat: Add type level impute function.
1 parent c445a0c commit 4ed1e0e

8 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/DataFrame/Typed.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ module DataFrame.Typed (
130130

131131
-- * Schema-modifying operations
132132
derive,
133+
impute,
133134
select,
134135
exclude,
135136
rename,
@@ -174,6 +175,7 @@ module DataFrame.Typed (
174175
ExcludeSchema,
175176
RenameInSchema,
176177
RemoveColumn,
178+
Impute,
177179
Append,
178180
Reverse,
179181
StripAllMaybe,
@@ -212,7 +214,6 @@ import DataFrame.Typed.Schema
212214
import DataFrame.Typed.TH (deriveSchema, deriveSchemaFromCsvFile)
213215
import DataFrame.Typed.Types (
214216
Column,
215-
TExpr (..),
216217
TSortOrder (..),
217218
These (..),
218219
TypedDataFrame,

src/DataFrame/Typed/Aggregate.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
2828

2929
import DataFrame.Internal.Column (Columnable)
3030
import qualified DataFrame.Internal.DataFrame as D
31-
import DataFrame.Internal.Expression (Expr, NamedExpr, UExpr (..))
31+
import DataFrame.Internal.Expression (NamedExpr)
3232
import qualified DataFrame.Operations.Aggregation as DA
3333

3434
import DataFrame.Typed.Freeze (unsafeFreeze)

src/DataFrame/Typed/Expr.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ module DataFrame.Typed.Expr (
8383
desc,
8484
) where
8585

86-
import Data.Kind (Type)
8786
import Data.Proxy (Proxy (..))
8887
import Data.String (IsString (..))
8988
import qualified Data.Text as T
@@ -100,7 +99,7 @@ import DataFrame.Internal.Expression (
10099
UnaryOp (..),
101100
)
102101
import DataFrame.Typed.Schema (AssertPresent, Lookup)
103-
import DataFrame.Typed.Types (Column, TExpr (..), TSortOrder (..))
102+
import DataFrame.Typed.Types (TExpr (..), TSortOrder (..))
104103

105104
-------------------------------------------------------------------------------
106105
-- Column reference — the core type-safe construction point

src/DataFrame/Typed/Freeze.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ module DataFrame.Typed.Freeze (
1515
unsafeFreeze,
1616
) where
1717

18-
import Data.Kind (Type)
19-
import qualified Data.Map as M
20-
import Data.Proxy (Proxy (..))
2118
import qualified Data.Text as T
22-
import Type.Reflection (SomeTypeRep, someTypeRep)
19+
import Type.Reflection (SomeTypeRep)
2320

2421
import qualified DataFrame.Internal.Column as C
2522
import qualified DataFrame.Internal.DataFrame as D

src/DataFrame/Typed/Join.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@ module DataFrame.Typed.Join (
1515
fullOuterJoin,
1616
) where
1717

18-
import Data.Proxy (Proxy (..))
19-
import qualified Data.Text as T
20-
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
18+
import GHC.TypeLits (Symbol)
2119

22-
import DataFrame.Internal.Column (Columnable)
23-
import qualified DataFrame.Internal.DataFrame as D
24-
import qualified DataFrame.Operations.Core as D
2520
import qualified DataFrame.Operations.Join as DJ
2621

27-
import DataFrame.Typed.Freeze (thaw, unsafeFreeze)
22+
import DataFrame.Typed.Freeze (unsafeFreeze)
2823
import DataFrame.Typed.Schema
2924
import DataFrame.Typed.Types (TypedDataFrame (..))
3025

src/DataFrame/Typed/Operations.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module DataFrame.Typed.Operations (
3030

3131
-- * Schema-modifying operations
3232
derive,
33+
impute,
3334
select,
3435
exclude,
3536
rename,
@@ -54,7 +55,6 @@ module DataFrame.Typed.Operations (
5455
(|>),
5556
) where
5657

57-
import qualified Data.Foldable as F
5858
import Data.Function ((&))
5959
import Data.Proxy (Proxy (..))
6060
import qualified Data.Text as T
@@ -63,9 +63,9 @@ import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
6363
import System.Random (RandomGen)
6464
import Prelude hiding (drop, filter, take)
6565

66+
import qualified DataFrame.Functions as DF
6667
import DataFrame.Internal.Column (Columnable)
6768
import qualified DataFrame.Internal.Column as C
68-
import DataFrame.Internal.Expression (Expr (..))
6969
import qualified DataFrame.Operations.Aggregation as DA
7070
import qualified DataFrame.Operations.Core as D
7171
import DataFrame.Operations.Merge ()
@@ -75,7 +75,7 @@ import qualified DataFrame.Operations.Transformations as D
7575

7676
-- Semigroup instance
7777

78-
import DataFrame.Typed.Freeze (thaw, unsafeFreeze)
78+
import DataFrame.Typed.Freeze (unsafeFreeze)
7979
import DataFrame.Typed.Schema
8080
import DataFrame.Typed.Types (TExpr (..), TSortOrder (..), TypedDataFrame (..))
8181
import qualified DataFrame.Typed.Types as T
@@ -222,6 +222,20 @@ derive (TExpr expr) (TDF df) = unsafeFreeze (D.derive colName expr df)
222222
where
223223
colName = T.pack (symbolVal (Proxy @name))
224224

225+
impute ::
226+
forall name a cols.
227+
( KnownSymbol name
228+
, Columnable a
229+
) =>
230+
a ->
231+
TypedDataFrame cols ->
232+
TypedDataFrame (Impute name cols)
233+
impute value (TDF df) =
234+
unsafeFreeze
235+
(D.derive colName (DF.fromMaybe value (DF.col @(Maybe a) colName)) df)
236+
where
237+
colName = T.pack (symbolVal (Proxy @name))
238+
225239
-- | Select a subset of columns by name.
226240
select ::
227241
forall (names :: [Symbol]) cols.

src/DataFrame/Typed/Schema.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module DataFrame.Typed.Schema (
1818
Lookup,
1919
HasName,
2020
RemoveColumn,
21+
Impute,
2122
SubsetSchema,
2223
ExcludeSchema,
2324
RenameInSchema,
@@ -60,9 +61,10 @@ import Data.Proxy (Proxy (..))
6061
import qualified Data.Text as T
6162
import Data.These (These)
6263
import GHC.TypeLits
63-
import Type.Reflection (SomeTypeRep, Typeable, someTypeRep, typeRep)
64+
import Type.Reflection (SomeTypeRep, Typeable, someTypeRep)
6465

6566
import DataFrame.Internal.Column (Columnable)
67+
import DataFrame.Internal.Types (If)
6668
import DataFrame.Typed.Types (Column)
6769

6870
-------------------------------------------------------------------------------
@@ -77,6 +79,14 @@ type family Lookup (name :: Symbol) (cols :: [Type]) :: Type where
7779
TypeError
7880
('Text "Column '" ':<>: 'Text name ':<>: 'Text "' not found in schema")
7981

82+
type family Impute (name :: Symbol) (cols :: [Type]) :: [Type] where
83+
Impute name (Column name (Maybe a) ': rest) = Column name a ': rest
84+
Impute name (Column name _ ': rest) =
85+
TypeError
86+
('Text "Column '" ':<>: 'Text name ':<>: 'Text "' is not of kind Maybe *")
87+
Impute name (col ': rest) = col ': Impute name rest
88+
Impute name '[] = '[]
89+
8090
-- | Add type to the end of a list.
8191
type family Snoc (xs :: [k]) (x :: k) :: [k] where
8292
Snoc '[] x = '[x]
@@ -108,11 +118,6 @@ type family ExcludeSchema (names :: [Symbol]) (cols :: [Type]) :: [Type] where
108118
(ExcludeSchema names rest)
109119
(Column n a ': ExcludeSchema names rest)
110120

111-
-- | Type-level if
112-
type family If (b :: Bool) (t :: k) (f :: k) :: k where
113-
If 'True t _ = t
114-
If 'False _ f = f
115-
116121
-- | Type-level elem for Symbols
117122
type family IsElem (x :: Symbol) (xs :: [Symbol]) :: Bool where
118123
IsElem x '[] = 'False

src/DataFrame/Typed/TH.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import qualified Data.Map as M
2020
import qualified Data.Text as T
2121

2222
import Language.Haskell.TH
23-
import Language.Haskell.TH.Syntax (Lift (..))
2423

2524
import qualified DataFrame.IO.CSV as D
2625
import qualified DataFrame.Internal.Column as C

0 commit comments

Comments
 (0)