Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Synopsis
- data Series v k a
- convert :: (Vector v1 a, Vector v2 a) => Series v1 k a -> Series v2 k a
- singleton :: Vector v a => k -> a -> Series v k a
- fromIndex :: Vector v a => (k -> a) -> Index k -> Series v k a
- fromList :: (Vector v a, Ord k) => [(k, a)] -> Series v k a
- toList :: Vector v a => Series v k a -> [(k, a)]
- fromVector :: (Ord k, Vector v k, Vector v a, Vector v (k, a)) => v (k, a) -> Series v k a
- toVector :: (Vector v a, Vector v k, Vector v (k, a)) => Series v k a -> v (k, a)
- data Occurrence
- fromListDuplicates :: (Vector v a, Ord k) => [(k, a)] -> Series v (k, Occurrence) a
- fromVectorDuplicates :: (Ord k, Vector v k, Vector v a, Vector v (k, a), Vector v (k, Occurrence)) => v (k, a) -> Series v (k, Occurrence) a
- fromStrictMap :: Vector v a => Map k a -> Series v k a
- toStrictMap :: Vector v a => Series v k a -> Map k a
- fromLazyMap :: Vector v a => Map k a -> Series v k a
- toLazyMap :: Vector v a => Series v k a -> Map k a
- class IsSeries t v k a where
- toSeries :: t -> Series v k a
- fromSeries :: Series v k a -> t
- map :: (Vector v a, Vector v b) => (a -> b) -> Series v k a -> Series v k b
- mapWithKey :: (Vector v a, Vector v b) => (k -> a -> b) -> Series v k a -> Series v k b
- mapIndex :: (Vector v a, Ord k, Ord g) => Series v k a -> (k -> g) -> Series v g a
- concatMap :: (Vector v a, Vector v k, Vector v b, Vector v (k, a), Vector v (k, b), Ord k) => (a -> Series v k b) -> Series v k a -> Series v k b
- filter :: (Vector v a, Vector v Int, Ord k) => (a -> Bool) -> Series v k a -> Series v k a
- filterWithKey :: (Vector v a, Vector v Int, Vector v Bool, Ord k) => (k -> a -> Bool) -> Series v k a -> Series v k a
- take :: Vector v a => Int -> Series v k a -> Series v k a
- takeWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
- drop :: Vector v a => Int -> Series v k a -> Series v k a
- dropWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
- mapWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => (k -> a -> m b) -> Series v k a -> m (Series v k b)
- mapWithKeyM_ :: (Vector v a, Monad m) => (k -> a -> m b) -> Series v k a -> m ()
- forWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => Series v k a -> (k -> a -> m b) -> m (Series v k b)
- forWithKeyM_ :: (Vector v a, Monad m) => Series v k a -> (k -> a -> m b) -> m ()
- traverseWithKey :: (Applicative t, Ord k, Traversable v, Vector v a, Vector v b, Vector v k, Vector v (k, a), Vector v (k, b)) => (k -> a -> t b) -> Series v k a -> t (Series v k b)
- fold :: Vector v a => Fold a b -> Series v k a -> b
- foldM :: (Monad m, Vector v a) => FoldM m a b -> Series v k a -> m b
- foldWithKey :: (Vector v a, Vector v k, Vector v (k, a)) => Fold (k, a) b -> Series v k a -> b
- foldMWithKey :: (Monad m, Vector v a, Vector v k, Vector v (k, a)) => FoldM m (k, a) b -> Series v k a -> m b
- foldMap :: (Monoid m, Vector v a) => (a -> m) -> Series v k a -> m
- foldMapWithKey :: (Monoid m, Vector v a, Vector v k, Vector v (k, a)) => (k -> a -> m) -> Series v k a -> m
- mean :: Fractional a => Fold a a
- variance :: Fractional a => Fold a a
- std :: Floating a => Fold a a
- length :: Vector v a => Series v k a -> Int
- null :: Vector v a => Series v k a -> Bool
- all :: Vector v a => (a -> Bool) -> Series v k a -> Bool
- any :: Vector v a => (a -> Bool) -> Series v k a -> Bool
- and :: Vector v Bool => Series v k Bool -> Bool
- or :: Vector v Bool => Series v k Bool -> Bool
- sum :: (Num a, Vector v a) => Series v k a -> a
- product :: (Num a, Vector v a) => Series v k a -> a
- maximum :: (Ord a, Vector v a) => Series v k a -> Maybe a
- maximumOn :: (Ord b, Vector v a) => (a -> b) -> Series v k a -> Maybe a
- minimum :: (Ord a, Vector v a) => Series v k a -> Maybe a
- minimumOn :: (Ord b, Vector v a) => (a -> b) -> Series v k a -> Maybe a
- argmax :: (Ord a, Vector v a) => Series v k a -> Maybe k
- argmin :: (Ord a, Vector v a, Vector v (Down a)) => Series v k a -> Maybe k
- postscanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> Series v k b -> Series v k a
- prescanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> Series v k b -> Series v k a
- forwardFill :: (Vector v a, Vector v (Maybe a)) => a -> Series v k (Maybe a) -> Series v k a
- zipWith :: (Vector v a, Vector v b, Vector v c, Vector v (Maybe c), Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k (Maybe c)
- zipWithMatched :: (Vector v a, Vector v b, Vector v c, Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k c
- zipWithKey :: (Vector v a, Vector v b, Vector v c, Vector v k, Ord k) => (k -> a -> b -> c) -> Series v k a -> Series v k b -> Series v k c
- zipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v (Maybe d), Ord k) => (a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k (Maybe d)
- zipWithMatched3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Ord k) => (a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k d
- zipWithKey3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v k, Ord k) => (k -> a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k d
- type ZipStrategy k a b = k -> a -> Maybe b
- skipStrategy :: ZipStrategy k a b
- mapStrategy :: (a -> b) -> ZipStrategy k a b
- constStrategy :: b -> ZipStrategy k a b
- zipWithStrategy :: (Vector v a, Vector v b, Vector v c, Ord k) => (a -> b -> c) -> ZipStrategy k a c -> ZipStrategy k b c -> Series v k a -> Series v k b -> Series v k c
- zipWithStrategy3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Ord k) => (a -> b -> c -> d) -> ZipStrategy k a d -> ZipStrategy k b d -> ZipStrategy k c d -> Series v k a -> Series v k b -> Series v k c -> Series v k d
- zipWithMonoid :: (Monoid a, Monoid b, Vector v a, Vector v b, Vector v c, Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k c
- esum :: (Ord k, Num a, Vector v a, Vector v (Sum a)) => Series v k a -> Series v k a -> Series v k a
- eproduct :: (Ord k, Num a, Vector v a, Vector v (Product a)) => Series v k a -> Series v k a -> Series v k a
- unzip :: (Vector v a, Vector v b, Vector v (a, b)) => Series v k (a, b) -> (Series v k a, Series v k b)
- unzip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => Series v k (a, b, c) -> (Series v k a, Series v k b, Series v k c)
- require :: (Vector v a, Vector v (Maybe a), Ord k) => Series v k a -> Index k -> Series v k (Maybe a)
- requireWith :: (Vector v a, Vector v b, Ord k) => (k -> b) -> (a -> b) -> Series v k a -> Index k -> Series v k b
- catMaybes :: (Vector v a, Vector v (Maybe a), Vector v Int, Ord k) => Series v k (Maybe a) -> Series v k a
- dropIndex :: Series v k a -> Series v Int a
- select :: (Selection s, Vector v a, Ord k) => Series v k a -> s k -> Series v k a
- selectWhere :: (Vector v a, Vector v Int, Vector v Bool, Ord k) => Series v k a -> Series v k Bool -> Series v k a
- data Range k
- to :: Ord k => k -> k -> Range k
- from :: k -> Range k
- upto :: k -> Range k
- class Selection s
- at :: (Vector v a, Ord k) => Series v k a -> k -> Maybe a
- iat :: Vector v a => Series v k a -> Int -> Maybe a
- replace :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a
- (|->) :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a
- (<-|) :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a
- groupBy :: Series v k a -> (k -> g) -> Grouping k g v a
- data Grouping k g v a
- aggregateWith :: (Ord g, Vector v a, Vector v b) => Grouping k g v a -> (Series v k a -> b) -> Series v g b
- foldWith :: (Ord g, Vector v a) => Grouping k g v a -> (a -> a -> a) -> Series v g a
- windowing :: (Ord k, Vector v a, Vector v b) => (k -> Range k) -> (Series v k a -> b) -> Series v k a -> Series v k b
- expanding :: (Vector v a, Vector v b) => Series v k a -> (Series v k a -> b) -> Series v k b
- display :: (Vector v a, Show k, Show a) => Series v k a -> String
- displayWith :: Vector v a => DisplayOptions k a -> Series v k a -> String
- noLongerThan :: (a -> String) -> Int -> a -> String
- data DisplayOptions k a = DisplayOptions {
- maximumNumberOfRows :: Int
- indexHeader :: String
- valuesHeader :: String
- keyDisplayFunction :: k -> String
- valueDisplayFunction :: a -> String
- defaultDisplayOptions :: (Show k, Show a) => DisplayOptions k a
Definition
A Series v k a
is a labeled array of type v
filled with values of type a
,
indexed by keys of type k
.
Like Map
, they support efficient:
- random access by key ( \(O(\log n)\) );
- slice by key ( \(O(\log n)\) ).
Like Vector
, they support efficient:
- random access by index ( \(O(1)\) );
- slice by index ( \(O(1)\) );
- numerical operations.
Instances
convert :: (Vector v1 a, Vector v2 a) => Series v1 k a -> Series v2 k a Source #
\(O(n)\) Convert between two types of Series
.
Building/converting Series
singleton :: Vector v a => k -> a -> Series v k a Source #
\(O(1)\) Create a Series
with a single element.
fromIndex :: Vector v a => (k -> a) -> Index k -> Series v k a Source #
\(O(n)\) Generate a Series
by mapping every element of its index.
Lists
fromList :: (Vector v a, Ord k) => [(k, a)] -> Series v k a Source #
Construct a Series
from a list of key-value pairs. There is no
condition on the order of pairs. Duplicate keys are silently dropped. If you
need to handle duplicate keys, see fromListDuplicates
.
toList :: Vector v a => Series v k a -> [(k, a)] Source #
Construct a list from key-value pairs. The elements are in order sorted by key.
Vectors
fromVector :: (Ord k, Vector v k, Vector v a, Vector v (k, a)) => v (k, a) -> Series v k a Source #
Construct a Series
from a Vector
of key-value pairs. There is no
condition on the order of pairs. Duplicate keys are silently dropped. If you
need to handle duplicate keys, see fromVectorDuplicates
.
Note that due to differences in sorting,
fromList
and
may not be equivalent if the input list contains duplicate keys.fromVector
. fromList
toVector :: (Vector v a, Vector v k, Vector v (k, a)) => Series v k a -> v (k, a) Source #
Construct a Vector
of key-value pairs. The elements are in order sorted by key.
Handling duplicates
data Occurrence Source #
Integer-like, non-negative number that specifies how many occurrences
of a key is present in a Series
.
The easiest way to convert from an Occurrence
to another integer-like type
is the fromIntegral
function.
Instances
fromListDuplicates :: (Vector v a, Ord k) => [(k, a)] -> Series v (k, Occurrence) a Source #
Construct a series from a list of key-value pairs.
Contrary to fromList
, values at duplicate keys are preserved. To keep each
key unique, an Occurrence
number counts up.
fromVectorDuplicates :: (Ord k, Vector v k, Vector v a, Vector v (k, a), Vector v (k, Occurrence)) => v (k, a) -> Series v (k, Occurrence) a Source #
Strict Maps
fromStrictMap :: Vector v a => Map k a -> Series v k a Source #
Construct a series from a strict Map
.
Lazy Maps
Ad-hoc conversion with other data structures
class IsSeries t v k a where Source #
The IsSeries
typeclass allow for ad-hoc definition
of conversion functions, converting to / from Series
.
toSeries :: t -> Series v k a Source #
Construct a Series
from some container of key-values pairs. There is no
condition on the order of pairs. Duplicate keys are silently dropped. If you
need to handle duplicate keys, see fromListDuplicates
or fromVectorDuplicates
.
fromSeries :: Series v k a -> t Source #
Construct a container from key-value pairs of a Series
.
The elements are returned in ascending order of keys.
Instances
Vector v a => IsSeries (IntMap a) (v :: Type -> Type) Int (a :: Type) Source # | |
(Ord k, Vector v a) => IsSeries (Seq (k, a)) (v :: Type -> Type) k (a :: Type) Source # | |
Vector v a => IsSeries (Set (k, a)) (v :: Type -> Type) k (a :: Type) Source # | |
Ord k => IsSeries (Vector (k, a)) Vector k (a :: Type) Source # | |
(Ord k, Unbox a, Unbox k) => IsSeries (Vector (k, a)) Vector k (a :: Type) Source # | |
(Ord k, Vector v a) => IsSeries [(k, a)] (v :: Type -> Type) k (a :: Type) Source # | |
Defined in Data.Series.Generic.Definition | |
Vector v a => IsSeries (Map k a) (v :: Type -> Type) k (a :: Type) Source # | |
Mapping and filtering
map :: (Vector v a, Vector v b) => (a -> b) -> Series v k a -> Series v k b Source #
\(O(n)\) Map every element of a Series
.
mapWithKey :: (Vector v a, Vector v b) => (k -> a -> b) -> Series v k a -> Series v k b Source #
\(O(n)\) Map every element of a Series
, possibly using the key as well.
mapIndex :: (Vector v a, Ord k, Ord g) => Series v k a -> (k -> g) -> Series v g a Source #
\(O(n \log n)\). Map each key in the index to another value. Note that the resulting series may have less elements, because each key must be unique.
In case new keys are conflicting, the first element is kept.
concatMap :: (Vector v a, Vector v k, Vector v b, Vector v (k, a), Vector v (k, b), Ord k) => (a -> Series v k b) -> Series v k a -> Series v k b Source #
filter :: (Vector v a, Vector v Int, Ord k) => (a -> Bool) -> Series v k a -> Series v k a Source #
Filter elements. Only elements for which the predicate is True
are kept.
Notice that the filtering is done on the values, not on the keys; see filterWithKey
to filter while taking keys into account.
filterWithKey :: (Vector v a, Vector v Int, Vector v Bool, Ord k) => (k -> a -> Bool) -> Series v k a -> Series v k a Source #
Filter elements, taking into account the corresponding key. Only elements for which
the predicate is True
are kept.
takeWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a Source #
\(O(n)\) Returns the longest prefix (possibly empty) of the input Series
that satisfy a predicate.
dropWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a Source #
\(O(n)\) Returns the complement of takeWhile
.
Mapping with effects
mapWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => (k -> a -> m b) -> Series v k a -> m (Series v k b) Source #
\(O(n)\) Apply the monadic action to every element of a series and its index, yielding a series of results.
mapWithKeyM_ :: (Vector v a, Monad m) => (k -> a -> m b) -> Series v k a -> m () Source #
\(O(n)\) Apply the monadic action to every element of a series and its index, discarding the results.
forWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => Series v k a -> (k -> a -> m b) -> m (Series v k b) Source #
\(O(n)\) Apply the monadic action to all elements of the series and their associated keys, yielding a series of results.
forWithKeyM_ :: (Vector v a, Monad m) => Series v k a -> (k -> a -> m b) -> m () Source #
\(O(n)\) Apply the monadic action to all elements of the series and their associated keys, discarding the results.
traverseWithKey :: (Applicative t, Ord k, Traversable v, Vector v a, Vector v b, Vector v k, Vector v (k, a), Vector v (k, b)) => (k -> a -> t b) -> Series v k a -> t (Series v k b) Source #
\(O(n)\) Traverse a Series
with an Applicative action, taking into account both keys and values.
Folding
fold :: Vector v a => Fold a b -> Series v k a -> b Source #
\(O(n)\) Execute a Fold
over a Series
.
See also foldM
for monadic folds, and foldWithKey
to take keys into
account while folding.
foldM :: (Monad m, Vector v a) => FoldM m a b -> Series v k a -> m b Source #
\(O(n)\) Execute a monadic FoldM
over a Series
.
See also fold
for pure folds, and foldMWithKey
to take keys into
account while folding.
foldWithKey :: (Vector v a, Vector v k, Vector v (k, a)) => Fold (k, a) b -> Series v k a -> b Source #
foldMWithKey :: (Monad m, Vector v a, Vector v k, Vector v (k, a)) => FoldM m (k, a) b -> Series v k a -> m b Source #
foldMap :: (Monoid m, Vector v a) => (a -> m) -> Series v k a -> m Source #
\(O(n)\) Fold over elements in a Series
.
foldMapWithKey :: (Monoid m, Vector v a, Vector v k, Vector v (k, a)) => (k -> a -> m) -> Series v k a -> m Source #
\(O(n)\) Fold over pairs of keys and elements in a Series
.
See also bifoldMap
.
Specialized folds
mean :: Fractional a => Fold a a #
Compute a numerically stable arithmetic mean of all elements
variance :: Fractional a => Fold a a #
Compute a numerically stable (population) variance over all elements
std :: Floating a => Fold a a #
Compute a numerically stable (population) standard deviation over all elements
all :: Vector v a => (a -> Bool) -> Series v k a -> Bool Source #
\(O(n)\) Check if all elements satisfy the predicate.
any :: Vector v a => (a -> Bool) -> Series v k a -> Bool Source #
\(O(n)\) Check if any element satisfies the predicate.
product :: (Num a, Vector v a) => Series v k a -> a Source #
\(O(n)\) Compute the product of the elements.
maximum :: (Ord a, Vector v a) => Series v k a -> Maybe a Source #
\(O(n)\) Yield the maximum element of the series. In case of a tie, the first occurrence wins.
minimum :: (Ord a, Vector v a) => Series v k a -> Maybe a Source #
\(O(n)\) Yield the minimum element of the series. In case of a tie, the first occurrence wins.
If the Series
is empty, Nothing
is returned.
argmax :: (Ord a, Vector v a) => Series v k a -> Maybe k Source #
\(O(n)\) Find the index of the maximum element in the input series.
If the input series is empty, Nothing
is returned.
The index of the first occurrence of the maximum element is returned.
>>>
import qualified Data.Series as Series
>>>
:{
let (xs :: Series.Series Int Int) = Series.fromList [ (1, 0) , (2, 1) , (3, 2) , (4, 7) , (5, 4) , (6, 5) ] in argmax xs :} Just 4
argmin :: (Ord a, Vector v a, Vector v (Down a)) => Series v k a -> Maybe k Source #
\(O(n)\) Find the index of the minimum element in the input series.
If the input series is empty, Nothing
is returned.
The index of the first occurrence of the minimum element is returned.
>>>
import qualified Data.Series as Series
>>>
:{
let (xs :: Series.Series Int Int) = Series.fromList [ (1, 1) , (2, 1) , (3, 2) , (4, 0) , (5, 4) , (6, 5) ] in argmin xs :} Just 4
Scans
postscanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> Series v k b -> Series v k a Source #
\(O(n)\) Left-to-right postscan.
>>>
import qualified Data.Vector as V
>>>
let xs = Series.fromList (zip [0..] [1,2,3,4]) :: Series V.Vector Int Int
>>>
xs
index | values ----- | ------ 0 | 1 1 | 2 2 | 3 3 | 4>>>
postscanl (+) 0 xs
index | values ----- | ------ 0 | 1 1 | 3 2 | 6 3 | 10
prescanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> Series v k b -> Series v k a Source #
\(O(n)\) Left-to-right prescan.
>>>
import qualified Data.Vector as V
>>>
let xs = Series.fromList (zip [0..] [1,2,3,4]) :: Series V.Vector Int Int
>>>
xs
index | values ----- | ------ 0 | 1 1 | 2 2 | 3 3 | 4>>>
prescanl (+) 0 xs
index | values ----- | ------ 0 | 0 1 | 1 2 | 3 3 | 6
:: (Vector v a, Vector v (Maybe a)) | |
=> a | Until the first non- |
-> Series v k (Maybe a) | |
-> Series v k a |
\(O(n)\) Replace all instances of Nothing
with the last previous
value which was not Nothing
.
>>>
import qualified Data.Vector as V
>>>
let xs = Series.fromList (zip [0..] [Just 1, Just 2,Nothing, Just 3]) :: Series V.Vector Int (Maybe Int)
>>>
xs
index | values ----- | ------ 0 | Just 1 1 | Just 2 2 | Nothing 3 | Just 3>>>
forwardFill 0 xs
index | values ----- | ------ 0 | 1 1 | 2 2 | 2 3 | 3
If the first entry of the series is missing, the first input to forwardFill
will be used:
>>>
let ys = Series.fromList (zip [0..] [Nothing, Just 2,Nothing, Just 3]) :: Series V.Vector Int (Maybe Int)
>>>
ys
index | values ----- | ------ 0 | Nothing 1 | Just 2 2 | Nothing 3 | Just 3>>>
forwardFill 0 ys
index | values ----- | ------ 0 | 0 1 | 2 2 | 2 3 | 3
Combining series
zipWith :: (Vector v a, Vector v b, Vector v c, Vector v (Maybe c), Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k (Maybe c) Source #
Apply a function elementwise to two series, matching elements
based on their keys. For keys present only in the left or right series,
the value Nothing
is returned.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
zipWith (+) xs ys
index | values ----- | ------ "alpha" | Just 10 "beta" | Just 12 "delta" | Nothing "gamma" | Nothing
To only combine elements where keys are in both series, see zipWithMatched
zipWithMatched :: (Vector v a, Vector v b, Vector v c, Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k c Source #
Apply a function elementwise to two series, matching elements based on their keys. Keys present only in the left or right series are dropped.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
zipWithMatched (+) xs ys
index | values ----- | ------ "alpha" | 10 "beta" | 12
To combine elements where keys are in either series, see zipWith
. To combine
three series, see zipWithMatched3
.
zipWithKey :: (Vector v a, Vector v b, Vector v c, Vector v k, Ord k) => (k -> a -> b -> c) -> Series v k a -> Series v k b -> Series v k c Source #
Apply a function elementwise to two series, matching elements based on their keys. Keys present only in the left or right series are dropped.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
zipWithKey (\k x y -> length k + x + y) xs ys
index | values ----- | ------ "alpha" | 15 "beta" | 16
To combine elements where keys are in either series, see zipWith
zipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v (Maybe d), Ord k) => (a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k (Maybe d) Source #
Apply a function elementwise to three series, matching elements
based on their keys. For keys present only in the left or right series,
the value Nothing
is returned.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
let zs = Series.fromList [ ("alpha", 20::Int), ("delta", 13), ("epsilon", 6) ]
>>>
zipWith3 (\x y z -> x + y + z) xs ys zs
index | values ----- | ------ "alpha" | Just 30 "beta" | Nothing "delta" | Nothing "epsilon" | Nothing "gamma" | Nothing
To only combine elements where keys are in all series, see zipWithMatched3
zipWithMatched3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Ord k) => (a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k d Source #
Apply a function elementwise to three series, matching elements based on their keys. Keys not present in all three series are dropped.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
let zs = Series.fromList [ ("alpha", 20::Int), ("delta", 13), ("epsilon", 6) ]
>>>
zipWithMatched3 (\x y z -> x + y + z) xs ys zs
index | values ----- | ------ "alpha" | 30
zipWithKey3 :: (Vector v a, Vector v b, Vector v c, Vector v d, Vector v k, Ord k) => (k -> a -> b -> c -> d) -> Series v k a -> Series v k b -> Series v k c -> Series v k d Source #
Apply a function elementwise to three series, matching elements based on their keys. Keys not present in all series are dropped.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
let zs = Series.fromList [ ("alpha", 20::Int), ("beta", 7), ("delta", 5) ]
>>>
zipWithKey3 (\k x y z -> length k + x + y + z) xs ys zs
index | values ----- | ------ "alpha" | 35 "beta" | 23
type ZipStrategy k a b = k -> a -> Maybe b Source #
A ZipStrategy
is a function which is used to decide what to do when a key is missing from one
of two Series
being zipped together with zipWithStrategy
.
If a ZipStrategy
returns Nothing
, the key is dropped.
If a ZipStrategy
returns
for key Just
vk
, then the value v
is inserted at key k
.
For example, the most basic ZipStrategy
is to skip over any key which is missing from the other series.
Such a strategy can be written as skip key value =
(see Nothing
skipStrategy
).
skipStrategy :: ZipStrategy k a b Source #
This ZipStrategy
drops keys which are not present in both Series
.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
zipWithStrategy (+) skipStrategy skipStrategy xs ys
index | values ----- | ------ "alpha" | 10 "beta" | 12
mapStrategy :: (a -> b) -> ZipStrategy k a b Source #
This ZipStrategy
sets the value at keys which are not present in both Series
to the some mapping from the value present in one of the series. See the example below.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 5::Int), ("beta", 6), ("delta", 7) ]
>>>
zipWithStrategy (+) (mapStrategy id) (mapStrategy (*10)) xs ys
index | values ----- | ------ "alpha" | 5 "beta" | 7 "delta" | 70 "gamma" | 2
constStrategy :: b -> ZipStrategy k a b Source #
This ZipStrategy
sets a constant value at keys which are not present in both Series
.
>>>
let xs = Series.fromList [ ("alpha", 0::Int), ("beta", 1), ("gamma", 2) ]
>>>
let ys = Series.fromList [ ("alpha", 10::Int), ("beta", 11), ("delta", 13) ]
>>>
zipWith (+) xs ys
index | values ----- | ------ "alpha" | Just 10 "beta" | Just 12 "delta" | Nothing "gamma" | Nothing>>>
zipWithStrategy (+) (constStrategy (-100)) (constStrategy 200) xs ys
index | values ----- | ------ "alpha" | 10 "beta" | 12 "delta" | 200 "gamma" | -100
:: (Vector v a, Vector v b, Vector v c, Ord k) | |
=> (a -> b -> c) | Function to combine values when present in both series |
-> ZipStrategy k a c | Strategy for when the key is in the left series but not the right |
-> ZipStrategy k b c | Strategy for when the key is in the right series but not the left |
-> Series v k a | |
-> Series v k b | |
-> Series v k c |
Zip two Series
with a combining function, applying a ZipStrategy
when one key is present in one of the Series
but not both.
Note that if you want to drop keys missing in either Series
, it is faster to use
than using zipWithMatched
f
.zipWithStrategy
f skipStrategy skipStrategy
:: (Vector v a, Vector v b, Vector v c, Vector v d, Ord k) | |
=> (a -> b -> c -> d) | Function to combine values when present in all series |
-> ZipStrategy k a d | Strategy for when the key is in the left series but not in all the others |
-> ZipStrategy k b d | Strategy for when the key is in the center series but not in all the others |
-> ZipStrategy k c d | Strategy for when the key is in the right series but not in all the others |
-> Series v k a | |
-> Series v k b | |
-> Series v k c | |
-> Series v k d |
Zip three Series
with a combining function, applying a ZipStrategy
when one key is
present in one of the Series
but not all of the others.
Note that if you want to drop keys missing in either Series
, it is faster to use
than using zipWithMatched3
f
.zipWithStrategy3
f skipStrategy skipStrategy skipStrategy
zipWithMonoid :: (Monoid a, Monoid b, Vector v a, Vector v b, Vector v c, Ord k) => (a -> b -> c) -> Series v k a -> Series v k b -> Series v k c Source #
Zip two Series
with a combining function. The value for keys which are missing from
either Series
is replaced with the appropriate mempty
value.
>>>
import Data.Monoid ( Sum(..) )
>>>
let xs = Series.fromList [ ("2023-01-01", Sum (1::Int)), ("2023-01-02", Sum 2) ]
>>>
let ys = Series.fromList [ ("2023-01-01", Sum (5::Int)), ("2023-01-03", Sum 7) ]
>>>
zipWith (<>) xs ys
index | values ----- | ------ "2023-01-01" | Just (Sum {getSum = 6}) "2023-01-02" | Nothing "2023-01-03" | Nothing>>>
zipWithMonoid (<>) xs ys
index | values ----- | ------ "2023-01-01" | Sum {getSum = 6} "2023-01-02" | Sum {getSum = 2} "2023-01-03" | Sum {getSum = 7}
esum :: (Ord k, Num a, Vector v a, Vector v (Sum a)) => Series v k a -> Series v k a -> Series v k a Source #
Elementwise sum of two Series
. Elements missing in one or the other Series
is considered 0.
>>>
let xs = Series.fromList [ ("2023-01-01", (1::Int)), ("2023-01-02", 2) ]
>>>
let ys = Series.fromList [ ("2023-01-01", (5::Int)), ("2023-01-03", 7) ]
>>>
xs `esum` ys
index | values ----- | ------ "2023-01-01" | 6 "2023-01-02" | 2 "2023-01-03" | 7
eproduct :: (Ord k, Num a, Vector v a, Vector v (Product a)) => Series v k a -> Series v k a -> Series v k a Source #
Elementwise product of two Series
. Elements missing in one or the other Series
is considered 1.
>>>
let xs = Series.fromList [ ("2023-01-01", (2::Int)), ("2023-01-02", 3) ]
>>>
let ys = Series.fromList [ ("2023-01-01", (5::Int)), ("2023-01-03", 7) ]
>>>
xs `eproduct` ys
index | values ----- | ------ "2023-01-01" | 10 "2023-01-02" | 3 "2023-01-03" | 7
unzip :: (Vector v a, Vector v b, Vector v (a, b)) => Series v k (a, b) -> (Series v k a, Series v k b) Source #
\(O(n)\) Unzip a Series
of 2-tuples.
unzip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => Series v k (a, b, c) -> (Series v k a, Series v k b, Series v k c) Source #
\(O(n)\) Unzip a Series
of 3-tuples.
Index manipulation
require :: (Vector v a, Vector v (Maybe a), Ord k) => Series v k a -> Index k -> Series v k (Maybe a) Source #
:: (Vector v a, Vector v b, Ord k) | |
=> (k -> b) | Function to apply to keys which are missing from the input series, but required in the input index |
-> (a -> b) | Function to apply to values which are in the input series and input index. |
-> Series v k a | |
-> Index k | |
-> Series v k b |
Generalization of require
, which maps missing keys to values.
This is particularly useful for Vector
instances which don't support Maybe
, like Data.Vector.Unboxed.
catMaybes :: (Vector v a, Vector v (Maybe a), Vector v Int, Ord k) => Series v k (Maybe a) -> Series v k a Source #
\(O(n)\) Only keep elements which are
. Just
v
dropIndex :: Series v k a -> Series v Int a Source #
Drop the index of a series by replacing it with an Int
-based index. Values will
be indexed from 0.
Accessors
Bulk access
select :: (Selection s, Vector v a, Ord k) => Series v k a -> s k -> Series v k a infixl 1 Source #
Select a subseries. There are two main ways to do this.
The first way to do this is to select a sub-series based on keys:
>>>
let xs = Series.fromList [('a', 10::Int), ('b', 20), ('c', 30), ('d', 40)]
>>>
xs `select` Index.fromList ['a', 'd']
index | values ----- | ------ 'a' | 10 'd' | 40
The second way to select a sub-series is to select all keys in a range:
>>>
xs `select` 'b' `to` 'c'
index | values ----- | ------ 'b' | 20 'c' | 30
Such ranges can also be unbounded. (i.e. all keys smaller or larger than some key), like so:
>>>
xs `select` upto 'c'
index | values ----- | ------ 'a' | 10 'b' | 20 'c' | 30>>>
xs `select` from 'c'
index | values ----- | ------ 'c' | 30 'd' | 40
Note that with select
, you'll always get a sub-series; if you ask for a key which is not
in the series, it'll be ignored:
>>>
xs `select` Index.fromList ['a', 'd', 'e']
index | values ----- | ------ 'a' | 10 'd' | 40
See require
if you want to ensure that all keys are present.
selectWhere :: (Vector v a, Vector v Int, Vector v Bool, Ord k) => Series v k a -> Series v k Bool -> Series v k a Source #
Select a sub-series from a series matching a condition.
Datatype representing an inclusive range of keys, which can either be bounded
or unbounded. The canonical ways to construct a Range
are to use to
, from
, and upto
:
>>>
'a' `to` 'z'
Range (from 'a' to 'z')>>>
from 'd'
Range (from 'd')>>>
upto 'q'
Range (up to 'q')
A Range
can be used to efficiently select a sub-series with select
.
Class for datatypes which can be used to select sub-series using select
.
There are two use-cases for select
:
- Bulk random-access (selecting from an
Index
of keys); - Bulk ordered access (selecting from a
Range
of keys).
See the documentation for select
.
Instances
Selection Set Source # | Selecting a sub-series from a |
Selection Range Source # | Selecting a sub-series based on a |
Selection Index Source # | |
Selection List Source # | Selecting a sub-series from a list is a convenience function. Internally, the list is converted to an index first. |
Single-element access
at :: (Vector v a, Ord k) => Series v k a -> k -> Maybe a Source #
\(O(\log n)\). Extract a single value from a series, by key.
iat :: Vector v a => Series v k a -> Int -> Maybe a Source #
\(O(1)\). Extract a single value from a series, by index.
Replacement
replace :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a Source #
Replace values from the right series with values from the left series at matching keys. Keys in the right series but not in the right series are unaffected.
(|->) :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a infix 6 Source #
Infix version of replace
(<-|) :: (Vector v a, Vector v Int, Ord k) => Series v k a -> Series v k a -> Series v k a infix 6 Source #
Flipped version of |->
,
Grouping and windowing operations
Group values in a Series
by some grouping function (k -> g
).
The provided grouping function is guaranteed to operate on a non-empty Series
.
This function is expected to be used in conjunction with aggregate
:
>>>
import Data.Maybe ( fromMaybe )
>>>
type Date = (Int, String)
>>>
month :: (Date -> String) = snd
>>>
:{
let xs = Series.fromList [ ((2020, "January") :: Date, 0 :: Int) , ((2021, "January"), -5) , ((2020, "June") , 20) , ((2021, "June") , 25) ] in xs `groupBy` month `aggregateWith` (fromMaybe 0 . minimum) :} index | values ----- | ------ "January" | -5 "June" | 20
aggregateWith :: (Ord g, Vector v a, Vector v b) => Grouping k g v a -> (Series v k a -> b) -> Series v g b Source #
Aggregate groups resulting from a call to groupBy
:
>>>
import Data.Maybe ( fromMaybe )
>>>
type Date = (Int, String)
>>>
month :: (Date -> String) = snd
>>>
:{
let xs = Series.fromList [ ((2020, "January") :: Date, 0 :: Int) , ((2021, "January"), -5) , ((2020, "June") , 20) , ((2021, "June") , 25) ] in xs `groupBy` month `aggregateWith` (fromMaybe 0 . minimum) :} index | values ----- | ------ "January" | -5 "June" | 20
If you want to aggregate groups using a binary function, see foldWith
which
may be much faster.
foldWith :: (Ord g, Vector v a) => Grouping k g v a -> (a -> a -> a) -> Series v g a Source #
Fold over each group in a Grouping
using a binary function.
While this is not as expressive as aggregateWith
, users looking for maximum
performance should use foldWith
as much as possible.
>>>
type Date = (Int, String)
>>>
month :: (Date -> String) = snd
>>>
:{
let xs = Series.fromList [ ((2020, "January") :: Date, 0 :: Int) , ((2021, "January"), -5) , ((2020, "June") , 20) , ((2021, "June") , 25) ] in xs `groupBy` month `foldWith` min :} index | values ----- | ------ "January" | -5 "June" | 20
windowing :: (Ord k, Vector v a, Vector v b) => (k -> Range k) -> (Series v k a -> b) -> Series v k a -> Series v k b Source #
General-purpose window aggregation.
>>>
import qualified Data.Series as Series
>>>
import Data.Series ( to )
>>>
:{
let (xs :: Series.Series Int Int) = Series.fromList [ (1, 0) , (2, 1) , (3, 2) , (4, 3) , (5, 4) , (6, 5) ] in windowing (\k -> k `to` (k + 2)) sum xs :} index | values ----- | ------ 1 | 3 2 | 6 3 | 9 4 | 12 5 | 9 6 | 5
:: (Vector v a, Vector v b) | |
=> Series v k a | Series vector |
-> (Series v k a -> b) | Aggregation function |
-> Series v k b | Resulting vector |
Expanding window aggregation.
>>>
import qualified Data.Series as Series
>>>
:{
let (xs :: Series.Series Int Int) = Series.fromList [ (1, 0) , (2, 1) , (3, 2) , (4, 3) , (5, 4) , (6, 5) ] in (xs `expanding` sum) :: Series.Series Int Int :} index | values ----- | ------ 1 | 0 2 | 1 3 | 3 4 | 6 5 | 10 6 | 15
Displaying Series
display :: (Vector v a, Show k, Show a) => Series v k a -> String Source #
Display a Series
using default DisplayOptions
.
displayWith :: Vector v a => DisplayOptions k a -> Series v k a -> String Source #
Display a Series
using customizable DisplayOptions
.
noLongerThan :: (a -> String) -> Int -> a -> String Source #
This function modifies existing functions to limit the width of its result.
>>>
let limit7 = (show :: Int -> String) `noLongerThan` 7
>>>
limit7 123456789
"123456..."
data DisplayOptions k a Source #
Options controlling how to display Series
in the displayWith
function.
Default options are provided by defaultDisplayOptions
.
To help with creating DisplayOptions
, see noLongerThan
.
DisplayOptions | |
|
defaultDisplayOptions :: (Show k, Show a) => DisplayOptions k a Source #
Default Series
display options.