Skip to content

Fix groupby bug #150

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

Merged
merged 7 commits into from
Mar 13, 2021
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.editorconfig
.editorconfig
node_modules
2 changes: 1 addition & 1 deletion danfojs-browser/lib/bundle.js

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions danfojs-browser/lib/bundle.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,6 @@
* =============================================================================
*/

/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

/**
* @license Complex.js v2.0.11 11/02/2016
*
Expand Down
2 changes: 1 addition & 1 deletion danfojs-browser/lib/bundle.js.map

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions danfojs-browser/src/core/groupby.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ export class GroupBy {
*/
col(col_names){

// if(!this.column_name.includes(col_name)){
// throw new Error(`Column ${col_name} does not exist in groups`)
// }

if (Array.isArray(col_names)){

for (let i = 0; i < col_names.length; i++){
Expand Down Expand Up @@ -179,7 +175,11 @@ export class GroupBy {
"cummin" : "cummin().values"
};
let is_array = false;

//the local variable to store variables to be used in eval
// this seems not to be needed in Node version, since local
//variable are easily accessed in the eval function
let local = null;

if (Array.isArray(operation)){
is_array = true;
}
Expand All @@ -201,10 +201,12 @@ export class GroupBy {
if (!ops_name.includes(op)){
throw new Error("operation does not exist");
}
data = eval(`this.group_col[key1][key2][i].${ops_map[op]}`);
local = this.group_col[key1][key2][i];
data = eval(`local.${ops_map[op]}`);

} else {
data = eval(`this.group_col[key1][key2][i].${operation}`);
local = this.group_col[key1][key2][i];
data = eval(`local.${operation}`);
}
count_group[key1][key2].push(data);

Expand All @@ -227,10 +229,12 @@ export class GroupBy {
if (!ops_name.includes(op)){
throw new Error("operation does not exist");
}
data = eval(`this.group_col[key1][i].${ops_map[op]}`);
local = this.group_col[key1][i];
data = eval(`local.${ops_map[op]}`);

} else {
data = eval(`this.group_col[key1][i].${operation}`);
local = this.group_col[key1][i];
data = eval(`local.${operation}`);
}

count_group[key1].push(data);
Expand Down
102 changes: 51 additions & 51 deletions danfojs-browser/tests/core/groupby.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("groupby", function () {
[ 20, 30, 1 ],
[ 39, 89, 1 ]
];

assert.deepEqual(group_df.col([ "C" ]).count().values, new_data);
});
it("sum column element in group", function () {
Expand Down Expand Up @@ -130,38 +130,38 @@ describe("groupby", function () {

assert.deepEqual(group_df.col([ "B", "C" ]).cumsum().values, new_data);
});
// it("cummulative max for groupby", function () {

// let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
// let cols = [ "A", "B", "C" ];
// let df = new dfd.DataFrame(data, { columns: cols });
// let group_df = df.groupby([ "A" ]);
// let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];
it("cummulative max for groupby", function () {

let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
let cols = [ "A", "B", "C" ];
let df = new dfd.DataFrame(data, { columns: cols });
let group_df = df.groupby([ "A" ]);
let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];


// assert.deepEqual(group_df.col([ "C" ]).cummax().values, new_data);
// });
// it("cummulative min for groupby", function () {
assert.deepEqual(group_df.col([ "C" ]).cummax().values, new_data);
});
it("cummulative min for groupby", function () {

// let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
// let cols = [ "A", "B", "C" ];
// let df = new dfd.DataFrame(data, { columns: cols });
// let group_df = df.groupby([ "A" ]);
// let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];
let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
let cols = [ "A", "B", "C" ];
let df = new dfd.DataFrame(data, { columns: cols });
let group_df = df.groupby([ "A" ]);
let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];

// assert.deepEqual(group_df.col([ "C" ]).cummin().values, new_data);
// });
assert.deepEqual(group_df.col([ "C" ]).cummin().values, new_data);
});

// it("cummulative prod for groupby", function () {
it("cummulative prod for groupby", function () {

// let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
// let cols = [ "A", "B", "C" ];
// let df = new dfd.DataFrame(data, { columns: cols });
// let group_df = df.groupby([ "A" ]);
// let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];
let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
let cols = [ "A", "B", "C" ];
let df = new dfd.DataFrame(data, { columns: cols });
let group_df = df.groupby([ "A" ]);
let new_data = [ [ 1, 3 ], [ 4, 6 ], [ 20, 40 ], [ 39, 78 ] ];

// assert.deepEqual(group_df.col([ "C" ]).cumprod().values, new_data);
// });
assert.deepEqual(group_df.col([ "C" ]).cumprod().values, new_data);
});
it("mean for groupby", function () {

let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
Expand All @@ -178,31 +178,31 @@ describe("groupby", function () {
assert.deepEqual(group_df.col([ "B", "C" ]).mean().values, new_data);
});

// it("printing multiindex table, example with cumsum operation for dataframe group by one column", function(){
// let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
// 'foo', 'bar', 'foo', 'foo' ],
// 'B': [ 'one', 'one', 'two', 'three',
// 'two', 'two', 'one', 'three' ],
// 'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
// 'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ] };


// let df = new dfd.DataFrame(data);

// let grp = df.groupby([ "A" ]);
// let rslt = [
// [ 'foo', 1 ],
// [ 'foo', 3 ],
// [ 'foo', 8 ],
// [ 'foo', 14 ],
// [ 'foo', 21 ],
// [ 'bar', 3 ],
// [ 'bar', 7 ],
// [ 'bar', 9 ]
// ];
// assert.deepEqual(grp.col([ "C" ]).cumsum().values, rslt);

// });
it("printing multiindex table, example with cumsum operation for dataframe group by one column", function(){
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo' ],
'B': [ 'one', 'one', 'two', 'three',
'two', 'two', 'one', 'three' ],
'C': [ 1, 3, 2, 4, 5, 2, 6, 7 ],
'D': [ 3, 2, 4, 1, 5, 6, 7, 8 ] };


let df = new dfd.DataFrame(data);

let grp = df.groupby([ "A" ]);
let rslt = [
[ 'foo', 1 ],
[ 'foo', 3 ],
[ 'foo', 8 ],
[ 'foo', 14 ],
[ 'foo', 21 ],
[ 'bar', 3 ],
[ 'bar', 7 ],
[ 'bar', 9 ]
];
assert.deepEqual(grp.col([ "C" ]).cumsum().values, rslt);

});
it("printing multiindex table, example with cumsum operation for dataframe group by one column", function(){
let data = { 'A': [ 'foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo' ],
Expand Down