Skip to content

Commit def7660

Browse files
committed
update groupby test
1 parent 9d8c6fb commit def7660

1 file changed

Lines changed: 122 additions & 15 deletions

File tree

src/danfojs-node/test/aggregators/groupby.test.js

Lines changed: 122 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ describe("groupby", function () {
217217
let df = new DataFrame(data);
218218
let group_df = df.groupby([ "A"]);
219219
let rslt = [
220-
[ 5, 3, 'foo' ],
221-
[ 6, 4, 'foo' ],
222-
[ 7, 7, 'foo' ],
223-
[ 9, 8, 'foo' ],
224-
[ 10, 9, 'foo' ],
225-
[ 4, 5, 'bar' ],
226-
[ 3, 6, 'bar' ],
227-
[ 8, 4, 'bar' ]
220+
[ 'foo', 5, 3 ],
221+
[ 'foo', 6, 4 ],
222+
[ 'foo', 7, 7 ],
223+
[ 'foo', 9, 8 ],
224+
[ 'foo', 10, 9 ],
225+
[ 'bar', 4, 5 ],
226+
[ 'bar', 3, 6 ],
227+
[ 'bar', 8, 4 ]
228228
]
229229
assert.deepEqual(group_df.col(['D', 'C']).apply((x) => x.add(2)).values, rslt);
230230
});
@@ -240,13 +240,120 @@ describe("groupby", function () {
240240
let df = new DataFrame(data);
241241
let group_df = df.groupby([ "A", "B"]);
242242
let rslt = [
243-
[ 2, 2, 2, 2, 'foo', 'one' ],
244-
[ 2, 2, 2, 2, 'foo', 'two' ],
245-
[ 1, 1, 1, 1, 'foo', 'three' ],
246-
[ 1, 1, 1, 1, 'bar', 'one' ],
247-
[ 1, 1, 1, 1, 'bar', 'three' ],
248-
[ 1, 1, 1, 1, 'bar', 'two' ]
249-
];
243+
[ 'foo', 'one', 2, 2, 2, 2 ],
244+
[ 'foo', 'two', 2, 2, 2, 2 ],
245+
[ 'foo', 'three', 1, 1, 1, 1 ],
246+
[ 'bar', 'one', 1, 1, 1, 1 ],
247+
[ 'bar', 'three', 1, 1, 1, 1 ],
248+
[ 'bar', 'two', 1, 1, 1, 1 ]
249+
]
250250
assert.deepEqual(group_df.apply((x) => x.count({axis:0})).values, rslt);
251251
});
252+
253+
it("should obtain the number of groups", function () {
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+
};
261+
let df = new DataFrame(data);
262+
let group_df = df.groupby([ "A", "B"]);
263+
let rslt = 6
264+
assert.equal(group_df.ngroups, rslt);
265+
});
266+
it("should obtain all groups", function () {
267+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
268+
'foo', 'bar', 'foo', 'foo' ],
269+
'B': [ 'one', 'one', 'two', 'three',
270+
'two', 'two', 'one', 'three' ],
271+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
272+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ]
273+
};
274+
let df = new DataFrame(data);
275+
let group_df = df.groupby([ "A", "B"]);
276+
let rslt = {
277+
'foo-one': {
278+
A: [ 'foo', 'foo' ],
279+
B: [ 'one', 'one' ],
280+
C: [ 1, 6 ],
281+
D: [ 3, 7 ]
282+
},
283+
'bar-one': { A: [ 'bar' ], B: [ 'one' ], C: [ 3 ], D: [ 2 ] },
284+
'foo-two': {
285+
A: [ 'foo', 'foo' ],
286+
B: [ 'two', 'two' ],
287+
C: [ 2, 5 ],
288+
D: [ 4, 5 ]
289+
},
290+
'bar-three': { A: [ 'bar' ], B: [ 'three' ], C: [ 4 ], D: [ 1 ] },
291+
'bar-two': { A: [ 'bar' ], B: [ 'two' ], C: [ 2 ], D: [ 6 ] },
292+
'foo-three': { A: [ 'foo' ], B: [ 'three' ], C: [ 7 ], D: [ 8 ] }
293+
}
294+
assert.deepEqual(group_df.groups, rslt);
295+
});
296+
297+
it("should obtain the first row of all groups", function () {
298+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
299+
'foo', 'bar', 'foo', 'foo' ],
300+
'B': [ 'one', 'one', 'one', 'three',
301+
'two', 'two', 'one', 'three' ],
302+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
303+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ]
304+
};
305+
let df = new DataFrame(data);
306+
let group_df = df.groupby([ "A", "B"]);
307+
let rslt = [
308+
[ 'foo', 'one', 'foo', 'one', 1, 3 ],
309+
[ 'foo', 'two', 'foo', 'two', 5, 5 ],
310+
[ 'foo', 'three', 'foo', 'three', 7, 8 ],
311+
[ 'bar', 'one', 'bar', 'one', 3, 2 ],
312+
[ 'bar', 'three', 'bar', 'three', 4, 1 ],
313+
[ 'bar', 'two', 'bar', 'two', 2, 6 ]
314+
]
315+
assert.deepEqual(group_df.first().values, rslt);
316+
});
317+
318+
it("should obtain the last row of all groups", function () {
319+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
320+
'foo', 'bar', 'foo', 'foo' ],
321+
'B': [ 'one', 'one', 'one', 'three',
322+
'two', 'two', 'one', 'three' ],
323+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
324+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ]
325+
};
326+
let df = new DataFrame(data);
327+
let group_df = df.groupby([ "A", "B"]);
328+
let rslt = [
329+
[ 'foo', 'one', 'foo', 'one', 6, 7 ],
330+
[ 'foo', 'two', 'foo', 'two', 5, 5 ],
331+
[ 'foo', 'three', 'foo', 'three', 7, 8 ],
332+
[ 'bar', 'one', 'bar', 'one', 3, 2 ],
333+
[ 'bar', 'three', 'bar', 'three', 4, 1 ],
334+
[ 'bar', 'two', 'bar', 'two', 2, 6 ]
335+
]
336+
assert.deepEqual(group_df.last().values, rslt);
337+
});
338+
339+
it("should obtain the number of rows of each groups", function () {
340+
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
341+
'foo', 'bar', 'foo', 'foo' ],
342+
'B': [ 'one', 'one', 'one', 'three',
343+
'two', 'two', 'one', 'three' ],
344+
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
345+
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ]
346+
};
347+
let df = new DataFrame(data);
348+
let group_df = df.groupby([ "A", "B"]);
349+
let rslt = [
350+
[ 'foo', 'one', 3 ],
351+
[ 'foo', 'two', 1 ],
352+
[ 'foo', 'three', 1 ],
353+
[ 'bar', 'one', 1 ],
354+
[ 'bar', 'three', 1 ],
355+
[ 'bar', 'two', 1 ]
356+
]
357+
assert.deepEqual(group_df.size().values, rslt);
358+
});
252359
})

0 commit comments

Comments
 (0)