forked from DataHaskell/dataframe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFrame.hs
More file actions
377 lines (324 loc) · 10 KB
/
Copy pathDataFrame.hs
File metadata and controls
377 lines (324 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
{- |
Module : DataFrame
Copyright : (c) 2025
License : GPL-3.0
Maintainer : mschavinda@gmail.com
Stability : experimental
Portability : POSIX
Batteries-included entry point for the DataFrame library.
This module re-exports the most commonly used pieces of the @dataframe@ library so you
can get productive fast in GHCi, IHaskell, or scripts.
__Naming convention__
* Use the @D.@ (\"DataFrame\") prefix for core table operations.
* Use the @F.@ (\"Functions\") prefix for the expression DSL (columns, math, aggregations).
Example session:
We provide a script that imports the core functionality and defines helpful
macros for writing safe code.
@
\$ cabal update
\$ cabal install dataframe
\$ dataframe
Configuring library for fake-package-0...
Warning: No exposed modules
GHCi, version 9.6.7: https:\/\/www.haskell.org\/ghc\/ :? for help
Loaded GHCi configuration from \/tmp\/cabal-repl.-242816\/setcwd.ghci
========================================
📦Dataframe
========================================
✨ Modules were automatically imported.
💡 Use prefix 'D' for core functionality.
● E.g. D.readCsv \"\/path\/to\/file\"
💡 Use prefix 'F' for expression functions.
● E.g. F.sum (F.col \@Int \"value\")
✅ Ready.
Loaded GHCi configuration from ./dataframe.ghci
ghci>
@
= Quick start
Load a CSV, select a few columns, filter, derive a column, then group + aggregate:
@
-- 1) Load data
ghci> df0 <- D.readCsv "data/housing.csv"
ghci> D.describeColumns df0
-------------------------------------------------------------------------------------------------------------
Column Name | # Non-null Values | # Null Values | # Partially parsed | # Unique Values | Type
--------------------|-------------------|---------------|--------------------|-----------------|-------------
Text | Int | Int | Int | Int | Text
--------------------|-------------------|---------------|--------------------|-----------------|-------------
ocean_proximity | 20640 | 0 | 0 | 5 | Text
median_house_value | 20640 | 0 | 0 | 3842 | Double
median_income | 20640 | 0 | 0 | 12928 | Double
households | 20640 | 0 | 0 | 1815 | Double
population | 20640 | 0 | 0 | 3888 | Double
total_bedrooms | 20640 | 0 | 0 | 1924 | Maybe Double
total_rooms | 20640 | 0 | 0 | 5926 | Double
housing_median_age | 20640 | 0 | 0 | 52 | Double
latitude | 20640 | 0 | 0 | 862 | Double
longitude | 20640 | 0 | 0 | 844 | Double
-- 2) Project & filter
ghci> :declareColumns df
ghci> df1 = D.filterWhere (ocean_proximity .== \"ISLAND\") df0 D.|> D.select [F.name median_house_value, F.name median_income, F.name ocean_proximity]
-- 3) Add a derived column using the expression DSL
-- (col types are explicit via TypeApplications)
ghci> df2 = D.derive "rooms_per_household" (total_rooms / households) df0
-- 4) Group + aggregate
ghci> import DataFrame.Operators
ghci> let grouped = D.groupBy ["ocean_proximity"] df0
ghci> let summary =
D.aggregate
[ F.maximum median_house_value \`as\` "max_house_value"]
grouped
ghci> D.take 5 summary
----------------------------------
ocean_proximity | max_house_value
-----------------|----------------
Text | Double
-----------------|----------------
<1H OCEAN | 500001.0
INLAND | 500001.0
ISLAND | 450000.0
NEAR BAY | 500001.0
NEAR OCEAN | 500001.0
@
== Simple operations (cheat sheet)
Most users only need a handful of verbs:
__I/O__
* @D.readCsv :: FilePath -> IO DataFrame@
* @D.readTsv :: FilePath -> IO DataFrame@
* @D.writeCsv :: FilePath -> DataFrame -> IO ()@
* @D.readParquet :: FilePath -> IO DataFrame@
* @D.readParquetWithOpts :: ParquetReadOptions -> FilePath -> IO DataFrame@
* @D.readParquetFiles :: FilePath -> IO DataFrame@
* @D.readParquetFilesWithOpts :: ParquetReadOptions -> FilePath -> IO DataFrame@
__Exploration__
* @D.take :: Int -> DataFrame -> DataFrame@
* @D.takeLast :: Int -> DataFrame -> DataFrame@
* @D.describeColumns :: DataFrame -> DataFrame@
* @D.summarize :: DataFrame -> DataFrame@
__Row ops__
* @D.filter :: Expr a -> (a -> Bool) -> DataFrame -> DataFrame@
* @D.filterWhere :: Expr Bool -> DataFrame -> DataFrame@
* @D.sortBy :: SortOrder -> [Text] -> DataFrame -> DataFrame@
__Column ops__
* @D.select :: [Text] -> DataFrame -> DataFrame@
* @D.exclude :: [Text] -> DataFrame -> DataFrame@
* @D.rename :: [(Text,Text)] -> DataFrame -> DataFrame@
* @D.derive :: Text -> D.Expr a -> DataFrame -> DataFrame@
__Group & aggregate__
* @D.groupBy :: [Text] -> DataFrame -> GroupedDataFrame@
* @D.aggregate :: [NamedExpr] -> GroupedDataFrame -> DataFrame@
__Joins__
* @D.innerJoin \/ D.leftJoin \/ D.rightJoin \/ D.fullOuterJoin@
== Expression DSL (F.*) at a glance
Columns (typed):
@
F.col \@Text "ocean_proximity"
F.col \@Double "total_rooms"
F.lit \@Double 1.0
@
Math & comparisons (overloaded by type):
@
(+), (-), (*), (/), abs, log, exp, round
(F.eq), (F.gt), (F.geq), (F.lt), (F.leq)
(.==), (.>), (.>=), (.<), (.<=)
@
Aggregations (for D.'aggregate'):
@
F.count \@a (F.col \@a "c")
F.sum \@Double (F.col \@Double "x")
F.mean \@Double (F.col \@Double "x")
F.min \@t (F.col \@t "x")
F.max \@t (F.col \@t "x")
@
== REPL power-tool: ':declareColumns'
Use @:declareColumns <df>@ in GHCi/IHaskell to turn each column of a bound 'DataFrame'
into a local binding with the same (mangled if needed) name and the column's concrete
vector type. This is great for quick ad-hoc analysis, plotting, or hand-rolled checks.
@
-- Suppose df has columns: "passengers" :: Int, "fare" :: Double, "payment" :: Text
ghci> :set -XTemplateHaskell
ghci> :declareColumns df
-- Now you have in scope:
ghci> :type passengers
passengers :: Expr Int
ghci> :type fare
fare :: Expr Double
ghci> :type payment
payment :: Expr Text
-- You can use them directly:
ghci> D.derive "fare_with_tip" (fare * 1.2)
@
Notes:
* Name mangling: spaces and non-identifier characters are replaced (e.g. @"trip id"@ -> @trip_id@).
* Optional/nullable columns are exposed as @Expr (Maybe a)@.
-}
module DataFrame (
-- * Core data structures
module Dataframe,
module Column,
module Row,
module Expression,
-- * Operator symbols.
module Operators,
-- * Display operations
module Display,
-- * Core dataframe operations
module Core,
-- * Types
module Schema,
-- * I/O
module CSV,
module UnstableCSV,
module Parquet,
module UnstableParquet,
-- * Type conversion
module Typing,
-- * Operations
module Subset,
module Transformations,
module Aggregation,
module Permutation,
module Merge,
module Join,
module Statistics,
-- * Errors
module Errors,
-- * Plotting
module Plot,
)
where
import DataFrame.Display as Display (
DisplayOptions (..),
defaultDisplayOptions,
display,
)
import DataFrame.Display.Terminal.Plot as Plot
import DataFrame.Errors as Errors
import DataFrame.IO.CSV as CSV (
HeaderSpec (..),
ReadOptions (..),
TypeSpec (..),
defaultReadOptions,
readCsv,
readCsvWithOpts,
readSeparated,
readTsv,
writeCsv,
writeSeparated,
)
import DataFrame.IO.Parquet as Parquet (
ParquetReadOptions (..),
defaultParquetReadOptions,
readParquet,
readParquetFiles,
readParquetFilesWithOpts,
readParquetWithOpts,
)
import DataFrame.IO.Unstable.CSV as UnstableCSV (
fastReadCsvUnstable,
fastReadTsvUnstable,
readCsvUnstable,
readTsvUnstable,
)
import DataFrame.IO.Unstable.Parquet as UnstableParquet (
readParquetUnstable,
)
import DataFrame.Internal.Column as Column (
Column,
fromList,
fromUnboxedVector,
fromVector,
hasElemType,
hasMissing,
isNumeric,
toList,
toVector,
)
import DataFrame.Internal.DataFrame as Dataframe (
DataFrame,
GroupedDataFrame,
empty,
null,
toMarkdownTable,
)
import DataFrame.Internal.Expression as Expression (Expr, prettyPrint)
import DataFrame.Internal.Row as Row (
Any,
Row,
fromAny,
rowValue,
toAny,
toRowList,
toRowVector,
)
import DataFrame.Internal.Schema as Schema (
makeSchema,
schemaType,
)
import DataFrame.Operations.Aggregation as Aggregation (
aggregate,
distinct,
groupBy,
)
import DataFrame.Operations.Core as Core hiding (
ColumnInfo (..),
nulls,
partiallyParsed,
renameSafe,
)
import DataFrame.Operations.Join as Join
import DataFrame.Operations.Merge as Merge
import DataFrame.Operations.Permutation as Permutation (
SortOrder (..),
shuffle,
sortBy,
)
import DataFrame.Operations.Statistics as Statistics (
correlation,
frequencies,
genericPercentile,
imputeWith,
interQuartileRange,
mean,
meanMaybe,
median,
medianMaybe,
percentile,
skewness,
standardDeviation,
sum,
summarize,
variance,
)
import DataFrame.Operations.Subset as Subset (
SelectionCriteria,
byIndexRange,
byName,
byNameProperty,
byNameRange,
byProperty,
cube,
drop,
dropLast,
exclude,
filter,
filterAllJust,
filterAllNothing,
filterBy,
filterJust,
filterNothing,
filterWhere,
kFolds,
randomSplit,
range,
sample,
select,
selectBy,
stratifiedSample,
stratifiedSplit,
take,
takeLast,
)
import DataFrame.Operations.Transformations as Transformations
import DataFrame.Operations.Typing as Typing
import DataFrame.Operators as Operators