Skip to content

Commit bce5175

Browse files
committed
Fix bug in groupby checking wrong colDtype
1 parent bdf0a24 commit bce5175

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/danfojs-base/aggregators/groupby.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ export default class Groupby {
262262
let colName = groupColNames[colKey]
263263
let colIndex = this.columnName.indexOf(colName)
264264
let colDtype = this.colDtype[colIndex]
265-
if (colDtype === "string") throw new Error(`Can't perform math operation on column ${colName}`)
265+
let operationVal = (typeof operation === "string") ? operation : operation[colName]
266+
if (colDtype === "string" && operationVal !== "count") throw new Error(`Can't perform math operation on column ${colName}`)
266267

267268
if (typeof operation === "string") {
268269
let colName2 = `${colName}_${operation}`

src/danfojs-base/core/frame.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,9 +3162,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
31623162
groupby(col: Array<string>): Groupby {
31633163
const columns = this.columns
31643164
const colIndex = col.map((val) => columns.indexOf(val))
3165-
const colDtype = this.dtypes.filter((val, index) => {
3166-
return colIndex.includes(index)
3167-
})
3165+
const colDtype = this.dtypes
31683166

31693167
return new Groupby(
31703168
col,

src/danfojs-browser/tests/aggregators/groupby.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,19 @@ describe("groupby", function () {
353353
];
354354
assert.deepEqual(group_df.size().values, rslt);
355355
});
356+
it("issue 396: fix groupby when first column is int", function () {
357+
let data = {
358+
'hours': [5, 6, 2, 8, 4, 3],
359+
'worker': ["david", "david", "john", "alice", "john", "david"],
360+
'day': ["monday", "tuesday", "wednesday", "thursday", "friday", "friday"]
361+
};
362+
let df = new dfd.DataFrame(data);
363+
let group_df = df.groupby(["worker"]);
364+
let rslt = [
365+
["david", 3, 3],
366+
["john", 2, 2],
367+
["alice", 1, 1]
368+
];
369+
assert.deepEqual(group_df.count().values, rslt);
370+
});
356371
});

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,20 @@ describe("groupby", function () {
364364
]
365365
assert.deepEqual(group_df.size().values, rslt);
366366
});
367+
368+
it("issue 396: fix groupby when first column is int", function () {
369+
let data = {
370+
'hours': [5, 6, 2, 8, 4, 3],
371+
'worker': ["david", "david", "john", "alice", "john", "david"],
372+
'day': ["monday", "tuesday", "wednesday", "thursday", "friday", "friday"]
373+
};
374+
let df = new DataFrame(data);
375+
let group_df = df.groupby(["worker"]);
376+
let rslt = [
377+
["david", 3, 3],
378+
["john", 2, 2],
379+
["alice", 1, 1]
380+
];
381+
assert.deepEqual(group_df.count().values, rslt);
382+
});
367383
})

0 commit comments

Comments
 (0)