Skip to content

Commit

Permalink
Merge pull request #398 from igonro/fix-groupby-bug
Browse files Browse the repository at this point in the history
Fix bug in groupby checking wrong colDtype
  • Loading branch information
steveoni authored Feb 23, 2022
2 parents bdf0a24 + bce5175 commit 5d8bd2d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/danfojs-base/aggregators/groupby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ export default class Groupby {
let colName = groupColNames[colKey]
let colIndex = this.columnName.indexOf(colName)
let colDtype = this.colDtype[colIndex]
if (colDtype === "string") throw new Error(`Can't perform math operation on column ${colName}`)
let operationVal = (typeof operation === "string") ? operation : operation[colName]
if (colDtype === "string" && operationVal !== "count") throw new Error(`Can't perform math operation on column ${colName}`)

if (typeof operation === "string") {
let colName2 = `${colName}_${operation}`
Expand Down
4 changes: 1 addition & 3 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3162,9 +3162,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
groupby(col: Array<string>): Groupby {
const columns = this.columns
const colIndex = col.map((val) => columns.indexOf(val))
const colDtype = this.dtypes.filter((val, index) => {
return colIndex.includes(index)
})
const colDtype = this.dtypes

return new Groupby(
col,
Expand Down
15 changes: 15 additions & 0 deletions src/danfojs-browser/tests/aggregators/groupby.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,19 @@ describe("groupby", function () {
];
assert.deepEqual(group_df.size().values, rslt);
});
it("issue 396: fix groupby when first column is int", function () {
let data = {
'hours': [5, 6, 2, 8, 4, 3],
'worker': ["david", "david", "john", "alice", "john", "david"],
'day': ["monday", "tuesday", "wednesday", "thursday", "friday", "friday"]
};
let df = new dfd.DataFrame(data);
let group_df = df.groupby(["worker"]);
let rslt = [
["david", 3, 3],
["john", 2, 2],
["alice", 1, 1]
];
assert.deepEqual(group_df.count().values, rslt);
});
});
16 changes: 16 additions & 0 deletions src/danfojs-node/test/aggregators/groupby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,20 @@ describe("groupby", function () {
]
assert.deepEqual(group_df.size().values, rslt);
});

it("issue 396: fix groupby when first column is int", function () {
let data = {
'hours': [5, 6, 2, 8, 4, 3],
'worker': ["david", "david", "john", "alice", "john", "david"],
'day': ["monday", "tuesday", "wednesday", "thursday", "friday", "friday"]
};
let df = new DataFrame(data);
let group_df = df.groupby(["worker"]);
let rslt = [
["david", 3, 3],
["john", 2, 2],
["alice", 1, 1]
];
assert.deepEqual(group_df.count().values, rslt);
});
})

0 comments on commit 5d8bd2d

Please sign in to comment.