{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module DataFrame.Typed.Aggregate (
groupBy,
agg,
aggNil,
aggregate,
aggregateUntyped,
) where
import Data.Proxy (Proxy (..))
import qualified Data.Text as T
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import DataFrame.Internal.Column (Columnable)
import qualified DataFrame.Internal.DataFrame as D
import DataFrame.Internal.Expression (NamedExpr)
import qualified DataFrame.Operations.Aggregation as DA
import DataFrame.Typed.Freeze (unsafeFreeze)
import DataFrame.Typed.Schema
import DataFrame.Typed.Types
groupBy ::
forall (keys :: [Symbol]) cols.
(AllKnownSymbol keys, AssertAllPresent keys cols) =>
TypedDataFrame cols -> TypedGrouped keys cols
groupBy :: forall (keys :: [Symbol]) (cols :: [*]).
(AllKnownSymbol keys, AssertAllPresent keys cols) =>
TypedDataFrame cols -> TypedGrouped keys cols
groupBy (TDF DataFrame
df) = GroupedDataFrame -> TypedGrouped keys cols
forall (keys :: [Symbol]) (cols :: [*]).
GroupedDataFrame -> TypedGrouped keys cols
TGD ([Text] -> DataFrame -> GroupedDataFrame
DA.groupBy (forall (names :: [Symbol]). AllKnownSymbol names => [Text]
symbolVals @keys) DataFrame
df)
aggNil :: TAgg keys cols '[]
aggNil :: forall (keys :: [Symbol]) (cols :: [*]). TAgg keys cols '[]
aggNil = TAgg keys cols '[]
forall (keys :: [Symbol]) (cols :: [*]). TAgg keys cols '[]
TAggNil
agg ::
forall name a keys cols aggs.
( KnownSymbol name
, Columnable a
) =>
TExpr cols a -> TAgg keys cols aggs -> TAgg keys cols (Column name a ': aggs)
agg :: forall (name :: Symbol) a (keys :: [Symbol]) (cols :: [*])
(aggs :: [*]).
(KnownSymbol name, Columnable a) =>
TExpr cols a
-> TAgg keys cols aggs -> TAgg keys cols (Column name a : aggs)
agg = Text
-> TExpr cols a
-> TAgg keys cols aggs
-> TAgg keys cols (Column name a : aggs)
forall a (cols :: [*]) (keys :: [Symbol]) (aggs1 :: [*])
(name :: Symbol).
Columnable a =>
Text
-> TExpr cols a
-> TAgg keys cols aggs1
-> TAgg keys cols (Column name a : aggs1)
TAggCons Text
colName
where
colName :: Text
colName = String -> Text
T.pack (Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name))
aggregate ::
forall keys cols aggs.
TAgg keys cols aggs ->
TypedGrouped keys cols ->
TypedDataFrame (Append (GroupKeyColumns keys cols) (Reverse aggs))
aggregate :: forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs
-> TypedGrouped keys cols
-> TypedDataFrame
(Append (GroupKeyColumns keys cols) (Reverse aggs))
aggregate TAgg keys cols aggs
tagg (TGD GroupedDataFrame
gdf) =
DataFrame
-> TypedDataFrame
(Append (GroupKeyColumns keys cols) (ReverseAcc aggs '[]))
forall (cols :: [*]). DataFrame -> TypedDataFrame cols
unsafeFreeze ([NamedExpr] -> GroupedDataFrame -> DataFrame
DA.aggregate (TAgg keys cols aggs -> [NamedExpr]
forall (keys :: [Symbol]) (cols :: [*]) (aggs :: [*]).
TAgg keys cols aggs -> [NamedExpr]
taggToNamedExprs TAgg keys cols aggs
tagg) GroupedDataFrame
gdf)
aggregateUntyped :: [NamedExpr] -> TypedGrouped keys cols -> D.DataFrame
aggregateUntyped :: forall (keys :: [Symbol]) (cols :: [*]).
[NamedExpr] -> TypedGrouped keys cols -> DataFrame
aggregateUntyped [NamedExpr]
exprs (TGD GroupedDataFrame
gdf) = [NamedExpr] -> GroupedDataFrame -> DataFrame
DA.aggregate [NamedExpr]
exprs GroupedDataFrame
gdf