Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in groupby checking wrong colDtype #398

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! This will fix a problem I ran into.


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);
});
})