Skip to content

Commit 55063b9

Browse files
committed
[groupby.js][l]: update groupby test in regards to the new update to suport data aggregation on groupby data and also test the .apply method implemented
1 parent 0531ee3 commit 55063b9

1 file changed

Lines changed: 66 additions & 3 deletions

File tree

danfojs-browser/tests/core/groupby.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("groupby", function () {
6565
[ 20, 30, 1 ],
6666
[ 39, 89, 1 ]
6767
];
68-
68+
6969
assert.deepEqual(group_df.col([ "C" ]).count().values, new_data);
7070
});
7171
it("sum column element in group", function () {
@@ -127,7 +127,6 @@ describe("groupby", function () {
127127
[ 20, 30, 30, 40 ],
128128
[ 39, 89, 89, 78 ]
129129
];
130-
131130
assert.deepEqual(group_df.col([ "B", "C" ]).cumsum().values, new_data);
132131
});
133132
it("cummulative max for groupby", function () {
@@ -137,7 +136,7 @@ describe("groupby", function () {
137136
let df = new dfd.DataFrame(data, { columns: cols });
138137
let group_df = df.groupby([ "A" ]);
139138
let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];
140-
139+
141140

142141
assert.deepEqual(group_df.col([ "C" ]).cummax().values, new_data);
143142
});
@@ -228,5 +227,69 @@ describe("groupby", function () {
228227
assert.deepEqual(grp.col([ "C", "D" ]).cumsum().values, rslt);
229228

230229
});
230+
it("should apply grouby operation to all column", function(){
231+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
232+
'foo', 'bar', 'foo', 'foo' ],
233+
'B': [ 'one', 'one', 'two', 'three',
234+
'two', 'two', 'one', 'three' ],
235+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
236+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ] };
237+
238+
let df = new dfd.DataFrame(data);
239+
240+
let grp = df.groupby([ "A", "B" ]);
241+
let rslt = [
242+
[ 'foo', 'one', 2, 2 ],
243+
[ 'foo', 'two', 2, 2 ],
244+
[ 'foo', 'three', 1, 1 ],
245+
[ 'bar', 'one', 1, 1 ],
246+
[ 'bar', 'two', 1, 1 ],
247+
[ 'bar', 'three', 1, 1 ]
248+
];
249+
250+
assert.deepEqual(grp.count().values, rslt);
251+
});
252+
it("should apply function to specific column", function () {
253+
254+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
255+
'foo', 'bar', 'foo', 'foo' ],
256+
'B': [ 'one', 'one', 'two', 'three',
257+
'two', 'two', 'one', 'three' ],
258+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
259+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ] };
260+
let df = new dfd.DataFrame(data);
261+
let group_df = df.groupby([ "A"]);
262+
let rslt = [
263+
[ 'foo', 5, 3 ], [ 'foo', 6, 4 ],
264+
[ 'foo', 7, 7 ], [ 'foo', 9, 8 ],
265+
[ 'foo', 10, 9 ], [ 'foo', 5, 3 ],
266+
[ 'foo', 6, 4 ], [ 'foo', 7, 7 ],
267+
[ 'foo', 9, 8 ], [ 'foo', 10, 9 ],
268+
[ 'bar', 4, 5 ], [ 'bar', 3, 6 ],
269+
[ 'bar', 8, 4 ], [ 'bar', 4, 5 ],
270+
[ 'bar', 3, 6 ], [ 'bar', 8, 4 ]
271+
];
272+
assert.deepEqual(group_df.col(['D', 'C']).apply((x) => x.add(2)).values, rslt);
273+
});
274+
it("should apply function to group column", function () {
275+
276+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
277+
'foo', 'bar', 'foo', 'foo' ],
278+
'B': [ 'one', 'one', 'two', 'three',
279+
'two', 'two', 'one', 'three' ],
280+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
281+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ] };
282+
let df = new dfd.DataFrame(data);
283+
let group_df = df.groupby([ "A", "B"]);
284+
let rslt = [
285+
[ 'foo', 'one', 2, 2 ],
286+
[ 'foo', 'two', 2, 2 ],
287+
[ 'foo', 'three', 1, 1 ],
288+
[ 'bar', 'one', 1, 1 ],
289+
[ 'bar', 'two', 1, 1 ],
290+
[ 'bar', 'three', 1, 1 ]
291+
];
292+
assert.deepEqual(group_df.apply((x) => x.count()).values, rslt);
293+
});
231294

232295
});

0 commit comments

Comments
 (0)