Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
VEBER Philippe
codepi
Commits
0f482db8
Commit
0f482db8
authored
Nov 03, 2020
by
Philippe Veber
Browse files
tk/Dataframe: fixes to the API
parent
0ba1aada
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
23 deletions
+28
-23
lib/toolbox/dataframe.ml
lib/toolbox/dataframe.ml
+19
-16
lib/toolbox/dataframe.mli
lib/toolbox/dataframe.mli
+9
-7
No files found.
lib/toolbox/dataframe.ml
View file @
0f482db8
...
...
@@ -2,12 +2,12 @@ open Core_kernel
open
Rresult
type
column
=
|
Int
of
int
array
|
Maybe_int
of
int
option
array
|
Float
of
float
array
|
Maybe_float
of
float
option
array
|
String
of
string
array
|
Maybe_string
of
string
option
array
|
Int
s
of
int
array
|
Int_opts
of
int
option
array
|
Float
s
of
float
array
|
Float_opts
of
float
option
array
|
String
s
of
string
array
|
String_opts
of
string
option
array
[
@@
deriving
show
]
type
t
=
{
...
...
@@ -19,7 +19,10 @@ type t = {
let
nrows
t
=
t
.
nrows
let
ncols
t
=
t
.
ncols
let
col
t
=
List
.
Assoc
.
find
t
.
cols
~
equal
:
String
.
equal
let
get_col_by_name
t
=
List
.
Assoc
.
find
t
.
cols
~
equal
:
String
.
equal
let
get_col
t
i
=
List
.
nth
t
.
cols
i
|>
Option
.
map
~
f
:
snd
let
parse_header
h
=
let
labels
=
String
.
split
~
on
:
'\t'
h
in
...
...
@@ -46,17 +49,17 @@ let rev_convert_col col =
in
let
conv_opt
f
=
conv
(
optionally
f
)
in
if
List
.
mem
col
"NA"
~
equal
:
String
.
equal
then
try
Maybe_int
(
conv_opt
Int
.
of_string
)
with
_
->
try
Maybe_float
(
conv_opt
Float
.
of_string
)
try
Int_opts
(
conv_opt
Int
.
of_string
)
with
_
->
try
Float_opts
(
conv_opt
Float
.
of_string
)
with
_
->
Maybe_string
(
String_opts
(
List
.
map
col
~
f
:
Option
.
some
|>
Array
.
of_list
)
else
try
Int
(
conv
Int
.
of_string
)
with
_
->
try
Float
(
conv
Float
.
of_string
)
with
_
->
String
(
Array
.
of_list_rev
col
)
try
Int
s
(
conv
Int
.
of_string
)
with
_
->
try
Float
s
(
conv
Float
.
of_string
)
with
_
->
String
s
(
Array
.
of_list_rev
col
)
let
parse_lines
ncols
lines
=
let
open
Result
.
Monad_infix
in
...
...
@@ -83,9 +86,9 @@ let%expect_test "Dataframe.parse_line ex1" =
print_endline
(
show_parse_result
got
)
;
[
%
expect
{
|
(
Ok
(
3
,
[(
Dataframe
.
String
[
|
"a"
;
"a"
;
"c"
|
]);
(
Dataframe
.
Float
[
|
1
.
2
;
1
.
2
;
-
1
.
2
|
]);
(
Dataframe
.
Maybe_int
[
|
None
;
(
Some
2
);
None
|
])]))
|
}]
[(
Dataframe
.
String
s
[
|
"a"
;
"a"
;
"c"
|
]);
(
Dataframe
.
Float
s
[
|
1
.
2
;
1
.
2
;
-
1
.
2
|
]);
(
Dataframe
.
Int_opts
[
|
None
;
(
Some
2
);
None
|
])]))
|
}]
let
from_file
path
=
let
open
Result
.
Monad_infix
in
...
...
lib/toolbox/dataframe.mli
View file @
0f482db8
type
t
type
column
=
|
Int
of
int
array
|
Maybe_int
of
int
option
array
|
Float
of
float
array
|
Maybe_float
of
float
option
array
|
String
of
string
array
|
Maybe_string
of
string
option
array
|
Int
s
of
int
array
|
Int_opts
of
int
option
array
|
Float
s
of
float
array
|
Float_opts
of
float
option
array
|
String
s
of
string
array
|
String_opts
of
string
option
array
val
nrows
:
t
->
int
val
ncols
:
t
->
int
val
col
:
t
->
string
->
column
option
val
get_col
:
t
->
int
->
column
option
val
get_col_by_name
:
t
->
string
->
column
option
val
from_file
:
string
->
(
t
,
[
>
`Msg
of
string
])
result
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment