Skip to content

Commit

Permalink
Fix bug with sorting.
Browse files Browse the repository at this point in the history
Index errors.
  • Loading branch information
mchav committed Jan 18, 2025
1 parent 8078dd8 commit 5e604eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Data/DataFrame/Internal/Row.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mkRowFromArgs names df i = V.map get (V.fromList names)
Just (UnboxedColumn column) -> toRowValue (column VU.! i)

mkRowRep :: DataFrame -> S.Set T.Text -> Int -> Row
mkRowRep df names i = V.generate (S.size names) (\index -> get index (names' V.! index))
mkRowRep df names i = V.generate (S.size names) (\index -> get (names' V.! index))
where
inOrderIndexes = map fst $ L.sortBy (compare `on` snd) $ M.toList (columnIndices df)
names' = V.fromList [n | n <- inOrderIndexes, S.member n names]
Expand All @@ -48,23 +48,23 @@ mkRowRep df names i = V.generate (S.size names) (\index -> get index (names' V.!
++ " has less items than "
++ "the other columns at index "
++ show i
get index name = case getColumn name df of
Just (BoxedColumn c) -> case c V.!? index of
get name = case getColumn name df of
Just (BoxedColumn c) -> case c V.!? i of
Just e -> toRowValue e
Nothing -> throwError name
Just (UnboxedColumn c) -> case c VU.!? index of
Just (UnboxedColumn c) -> case c VU.!? i of
Just e -> toRowValue e
Nothing -> throwError name
Just (GroupedBoxedColumn c) -> case c V.!? index of
Just (GroupedBoxedColumn c) -> case c V.!? i of
Just e -> toRowValue e
Nothing -> throwError name
Just (GroupedUnboxedColumn c) -> case c V.!? index of
Just (GroupedUnboxedColumn c) -> case c V.!? i of
Just e -> toRowValue e
Nothing -> throwError name

sortedIndexes' :: Bool -> V.Vector Row -> VU.Vector Int
sortedIndexes' asc rows = runST $ do
withIndexes <- VG.thaw (V.indexed rows)
VA.sortBy (\(a, b) (a', b') -> (if asc then compare else flip compare) b b') withIndexes
VA.sortBy ((if asc then compare else flip compare) `on` snd) withIndexes
sorted <- VG.unsafeFreeze withIndexes
return $ VU.generate (VG.length rows) (\i -> fst (sorted VG.! i))
5 changes: 5 additions & 0 deletions src/Data/DataFrame/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE Strict #-}
module Data.DataFrame.Internal.Types where

import Data.Int ( Int8, Int16, Int32, Int64 )
Expand Down Expand Up @@ -37,6 +38,10 @@ instance Ord RowValue where
Refl <- testEquality (typeOf a) (typeOf b)
return $ a <= b

instance Show RowValue where
show :: RowValue -> String
show (Value a) = show a

toRowValue :: forall a . (Columnable a) => a -> RowValue
toRowValue = Value

Expand Down

0 comments on commit 5e604eb

Please sign in to comment.