-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataframe.ijs
157 lines (138 loc) · 4.22 KB
/
dataframe.ijs
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
NB.=============================
NB. Initialsie tables/dataframe
cocurrent 'pdataframe'
Note 'Extending inverted table'
This script extends the utilities in general/misc/inverted for working with Inverted Tables
Potentially they could combine to become a new addon tables/inverted ?
)
require 'general/misc/inverted'
require 'general/misc/validate'
NB. see also tassert
isInverted=: (1 = [: #@~. #&>) *. (isscalar +. isvector) *. isboxed
NB.*tmakenumbcol v Convert columns of inverted table to numeric
tmakenumcol=: verb define
_9999 tmakenumcol y
:
NB. assert. isinverted y
tassert y
dat=. x&". &.> y
notnum=. x&e.@> dat NB. mask of boxes containing an error code
idx=. I. notnum NB. index of non-numeric columns
if. #idx do.
dat=. (idx{"1 y) idx}dat NB. amend non-numeric columns
end.
dat
)
NB.*tshow v Pretty-print a long inverted table
tshow=: verb define
tassert y
if. 20 < ttally y do.
((datatype , '---' , [: ":@,. 5&{.) , '...' , ([: ":@,. _5&{.))&.> y
else.
(datatype , '---' , ":@,.)&.> y
end.
)
Note 'Example Use'
load 'tables/dataframe/test/test' NB. expect value error (just defining tables)
] Bivt=. ifa }. B
tmakenumcol Bivt
tshow tmakenumcol Bivt
]Ivt=. (<1e6 + 21?100000), ifa 21 5 ?@$ 0 NB. create numeric Inverted table
tshow Ivt
tshow 3&}.&.> Ivt
)
NB.===========================================================
Note 'J DataFrames'
Utilities for working with J DataFrames
J DataFrames consist of an inverted tables laminated with boxed list of column names
)
NB.*isDataFrame v Checks if noun is a DataFrame
isDataFrame=: isInverted@{: *. (2 = #)
isTable=: ismatrix *. isboxed NB. table with boxed atoms
isArray=: ismatrix *. -.@isboxed NB. unboxed table
NB.*dfFromTable v Make a DataFrame from Table with a header row y
dfFromTable=: ({. ,: ifa@}.) :. dftoTable
NB.*dftoTable v Convert a DataFrame (y) to a Table with a header row
dftoTable=: ({. , afi@{:) :. dfFromTable
NB.*dfToArray v Catenate columns of DataFrame (y) as an unboxed table without headers
dfToArray=: >@(,.&.>/)@{:
NB.*dfFromArray v Convert columns of a simple table to columns of a Dataframe
dfFromArray=: [ ,: <"1@|:@]
NB.*defaultHdr v Create default list of boxed column names for a
defaultHdr=: ([: ('column_' , ":)&.> >:@i.)@{:@$
NB.*makeDataFrame v Create a Dataframe from a table representation (y) and optional header (x)
NB. y is: one of inverted-table, boxed-table, unboxed-array
NB. x is: optional list of boxed column names, or 1 if a boxed-table already has a header row
makeDataFrame=:{{
(defaultHdr makeDataFrame ]) y
:
tbltype=. I.@(isInverted , isTable, isArray) y
select. ,/ tbltype
case. 0 do.
df=. x ,: y
case. 1 do.
if. x = 1 do.
df=. dfFromTable y
else.
df=. dfFromTable x , y
end.
case. 2 do.
df=. x dfFromArray y
case. do.
echo 'Unsupported right argument'
df=. empty''
end.
df
}}
NB.*dfp a Apply inverted table verb u on DataFrame y
NB. u is a verb designed to work on an inverted table
NB. dfp will remove the column header, apply u y (or x u y)
NB. and then reapply the header row, if appropriate
dfp=: dfPipe=: {{
res=. u {:y
if. (isInverted res) +. tshow f.`'' -: u f.`'' do.
({.y) ,: res
end.
:
xa=. x
if. isDataFrame x do.
xa=. {:x
end.
res=. xa u {:y
if. isInverted res do.
({.y) ,: res
end.
}}
NB.*dfShow v Pretty-print a DataFrame
dfShow=: tshow dfp
NB.*dfSort v Sort a DataFrame
dfSort=: tsort dfp
NB.*dfSelect v Select column(s) (x) of a DataFrame (y)
dfSelect=: {{
if. (isinteger *. -.@isboxed) x do.
colidx=. x NB. left arg is column indexes
else.
colidx=. (boxopen x) (i.~ {.) y
end.
colidx {"1 y
}}
NB.*dfSelect v Drop column(s) (x) from a DataFrame (y)
dfDrop=: {{
if. (isinteger *. -.@isboxed) x do.
colidx=. x NB. left arg is column indexes
else.
colidx=. (boxopen x) (i.~ {.) y
end.
(<<<colidx) {"1 y
}}
Note 'Example use'
load 'tables/dataframe/test/test' NB. expect value error (just defining tables)
]Bdf=: dfFromTable B
tsort dfp Bdf
dfSort Bdf
tmakenumcol dfp tsort dfp Bdf
]Ivt=. (<1e6 + 21?100000), ifa 21 5 ?@$ 0 NB. create numeric Inverted table
tshow Ivt
tshow dfp makeDataFrame Ivt
dfShow makeDataFrame 3&}.&.> Ivt
)