Skip to content

Commit 3793a5d

Browse files
authored
Merge pull request #154 from opensource9ja/browser-groupby
Groupby update
2 parents 36b0cef + 10bb205 commit 3793a5d

File tree

14 files changed

+498
-200
lines changed

14 files changed

+498
-200
lines changed

danfojs-browser/lib/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

danfojs-browser/lib/bundle.js.LICENSE.txt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,6 @@
146146
* =============================================================================
147147
*/
148148

149-
/**
150-
* @license
151-
* Copyright 2021 Google LLC. All Rights Reserved.
152-
* Licensed under the Apache License, Version 2.0 (the "License");
153-
* you may not use this file except in compliance with the License.
154-
* You may obtain a copy of the License at
155-
*
156-
* http://www.apache.org/licenses/LICENSE-2.0
157-
*
158-
* Unless required by applicable law or agreed to in writing, software
159-
* distributed under the License is distributed on an "AS IS" BASIS,
160-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
161-
* See the License for the specific language governing permissions and
162-
* limitations under the License.
163-
* =============================================================================
164-
*/
165-
166149
/**
167150
* @license Complex.js v2.0.11 11/02/2016
168151
*

danfojs-browser/lib/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

danfojs-browser/src/core/concat.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class Concat {
1515

1616
let df_list = null; //set the df_list to null
1717
let axis = null; // set axis to null
18+
let indexes = null;
1819

1920
//check if df_list is an array
2021
if (Array.isArray(kwargs["df_list"])) {
@@ -41,13 +42,14 @@ export class Concat {
4142

4243

4344
let df_object = Object.assign({}, df_list); // convert the array to object
44-
45+
4546
if (axis == 1) {
4647

4748
let columns = [];
4849
let duplicate_col_count = {};
4950
let max_length = 0;
50-
51+
let a_key = Object.keys(df_object)[0];
52+
indexes = df_object[a_key].index;
5153
for (let key in df_object) {
5254

5355
let column = df_object[key].columns;
@@ -121,15 +123,22 @@ export class Concat {
121123
}
122124
}
123125

124-
let df = new DataFrame(data, { columns: columns }); //convert to dataframe
126+
let df = new DataFrame(data, { columns: columns, index: indexes }); //convert to dataframe
125127
return df;
126128
} else {
127129
//concatenate base on axis 0
128130
let columns = [];
129-
131+
let row_indexes = [];
132+
let col_i = 0;
130133
for (let key in df_list) {
131134
let column = df_list[key].columns;
132135
columns.push(...column);
136+
indexes = df_list[key].index;
137+
let r_index = indexes.map((val) => {
138+
return `${val}_row${col_i}`;
139+
});
140+
row_indexes.push(...r_index);
141+
col_i += 1;
133142
}
134143

135144
let column_set = new Set(columns);
@@ -187,10 +196,10 @@ export class Concat {
187196
}
188197

189198
if (Array.isArray(data[0])){
190-
let df = new DataFrame(data, { columns: columns });
199+
let df = new DataFrame(data, { columns: columns, index: row_indexes });
191200
return df;
192201
} else {
193-
let sf = new Series(data);
202+
let sf = new Series(data, { index: row_indexes });
194203
return sf;
195204
}
196205

danfojs-browser/src/core/groupby.js

Lines changed: 107 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DataFrame } from "./frame";
22
import { Utils } from "./utils";
3+
import { Series } from "./series";
34
const utils = new Utils;
45

56
/**
@@ -101,7 +102,7 @@ export class GroupBy {
101102
* @return Groupby data structure
102103
*/
103104
col(col_names){
104-
105+
this.selected_column = col_names; // store col_names for use later in .apply
105106
if (Array.isArray(col_names)){
106107

107108
for (let i = 0; i < col_names.length; i++){
@@ -115,44 +116,50 @@ export class GroupBy {
115116
throw new Error(`Col_name must be an array of column`);
116117
}
117118

118-
this.group_col_name = col_names; // store the column name
119+
// let group_col_name = col_names; // store the column name
120+
let group_col = {};
119121
if (this.key_col.length == 2){
120122

121-
this.group_col = {};
122-
123123
for (var key1 in this.data_tensors){
124124

125-
this.group_col[key1] = {};
125+
group_col[key1] = {};
126126
for (var key2 in this.data_tensors[key1]){
127127

128-
this.group_col[key1][key2] = [];
128+
group_col[key1][key2] = [];
129129
for (let i = 0; i < col_names.length; i++){
130130
let col_name = col_names[i];
131131
let data = this.data_tensors[key1][key2].column(col_name);
132-
this.group_col[key1][key2].push(data);
132+
group_col[key1][key2].push(data);
133133
}
134134

135135
}
136136
}
137137
} else {
138-
139-
this.group_col = {};
140-
141138
for (let key1 in this.data_tensors){
142139

143-
this.group_col[key1] = [];
140+
group_col[key1] = [];
144141
for (let i = 0; i < col_names.length; i++){
145142
let col_name = col_names[i];
146143
let data = this.data_tensors[key1].column(col_name);
147-
this.group_col[key1].push(data);
144+
group_col[key1].push(data);
148145
}
149146

150147
}
151148
}
152-
153-
return this;
149+
const gp = new GroupBy(
150+
null,
151+
this.key_col,
152+
null,
153+
col_names
154+
);
155+
156+
gp.group_col = group_col;
157+
gp.group_col_name = col_names;
158+
// return gp;
159+
return gp;
154160
}
155161

162+
156163
/**
157164
* Basic root of all column arithemetic in groups
158165
* @param {operation} operatioin String
@@ -178,8 +185,8 @@ export class GroupBy {
178185
//the local variable to store variables to be used in eval
179186
// this seems not to be needed in Node version, since local
180187
//variable are easily accessed in the eval function
181-
let local = null;
182-
188+
let local = null;
189+
183190
if (Array.isArray(operation)){
184191
is_array = true;
185192
}
@@ -248,70 +255,60 @@ export class GroupBy {
248255

249256
}
250257

258+
operations(ops, name) {
259+
if (!this.group_col) {
260+
let column = this.column_name.filter((val) => !this.key_col.includes(val));
261+
let col_gp = this.col(column);
262+
let value = col_gp.arithemetic(ops);
263+
let df = col_gp.to_DataFrame(col_gp.key_col, col_gp.group_col_name, value, name);
264+
return df;
265+
} else {
266+
let value = this.arithemetic(ops);
267+
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, name);
268+
return df;
269+
}
270+
}
251271
count(){
252-
253-
let value = this.arithemetic("count()");
254-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "count");
255-
return df;
272+
return this.operations("count()", "count");
256273
}
257274

258275
sum(){
259-
let value = this.arithemetic("sum()");
260-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "sum");
261-
return df;
276+
return this.operations("sum()", "sum");
262277
}
263278

264279
std(){
265-
let value = this.arithemetic("std()");
266-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "std");
267-
return df;
280+
return this.operations("std()", "std");
268281
}
269282

270283
var(){
271-
let value = this.arithemetic("var()");
272-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "var");
273-
return df;
284+
return this.operations("var()", "var");
274285
}
275286

276287
mean(){
277-
let value = this.arithemetic("mean()");
278-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "mean");
279-
return df;
288+
return this.operations("mean()", "mean");
280289
}
281290

282291
cumsum(){
283-
let value = this.arithemetic("cumsum().values");
284-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "cumsum");
285-
return df;
292+
return this.operations("cumsum().values", "cumsum");
286293
}
287294
cummax(){
288-
let value = this.arithemetic("cummax().values");
289-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "cummax");
290-
return df;
295+
return this.operations("cummax().values", "cummax");
291296
}
292297

293298
cumprod(){
294-
let value = this.arithemetic("cumprod().values");
295-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "cumprod");
296-
return df;
299+
return this.operations("cumprod().values", "cumprod");
297300
}
298301

299302
cummin(){
300-
let value = this.arithemetic("cummin().values");
301-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "cummin");
302-
return df;
303+
return this.operations("cummin().values", "cummin");
303304
}
304305

305306
max(){
306-
let value = this.arithemetic("max()");
307-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "max");
308-
return df;
307+
return this.operations("max()", "max");
309308
}
310309

311310
min(){
312-
let value = this.arithemetic("min()");
313-
let df = this.to_DataFrame(this.key_col, this.group_col_name, value, "min");
314-
return df;
311+
return this.operations("min()", "min");
315312
}
316313

317314
/**
@@ -350,17 +347,16 @@ export class GroupBy {
350347
let columns = Object.keys(kwargs);
351348
let operations = columns.map((x) => { return kwargs[x].toLocaleLowerCase(); });
352349

353-
this.col(columns);
350+
let col_gp = this.col(columns);
354351

355-
let data = this.arithemetic(operations);
356-
let df = this.to_DataFrame(this.key_col, this.group_col_name, data, operations);
352+
let data = col_gp.arithemetic(operations);
353+
let df = this.to_DataFrame(col_gp.key_col, col_gp.group_col_name, data, operations);
357354

358355
return df;
359356
}
360357

361358
to_DataFrame(key_col, col, data, ops){
362359

363-
// console.log(data);
364360
if (key_col.length == 2){
365361
let df_data = [];
366362
for (let key_1 in data){
@@ -455,4 +451,61 @@ export class GroupBy {
455451
}
456452
}
457453

454+
apply(callable){
455+
let df_data;
456+
let column;
457+
if (!this.group_col) {
458+
column = this.column_name.filter((val) => !this.key_col.includes(val));
459+
let col_gp = this.col(column);
460+
df_data = col_gp.group_col;
461+
} else {
462+
column = this.group_col_name;
463+
df_data = this.group_col;
464+
}
465+
let data = [];
466+
let count_group = {};
467+
if (this.key_col.length == 2) {
468+
469+
for (let key in this.data_tensors) {
470+
count_group[key] = {};
471+
for (let key2 in this.data_tensors[key]) {
472+
count_group[key][key2] = [];
473+
for (let i = 0; i < df_data[key][key2].length; i++ ) {
474+
let callable_rslt = callable(df_data[key][key2][i]);
475+
if (callable_rslt instanceof DataFrame) {
476+
count_group[key][key2].push(callable_rslt.values);
477+
} else {
478+
if (callable_rslt instanceof Series) {
479+
count_group[key][key2].push(callable_rslt.values);
480+
} else {
481+
count_group[key][key2].push(callable_rslt);
482+
}
483+
}
484+
}
485+
486+
487+
}
488+
}
489+
} else {
490+
for (let key in df_data) {
491+
count_group[key] = [];
492+
for (let i = 0; i < df_data[key].length; i++ ){
493+
let callable_rslt = callable(df_data[key][i]);
494+
if (callable_rslt instanceof DataFrame) {
495+
count_group[key].push(callable_rslt.values);
496+
} else {
497+
if (callable_rslt instanceof Series) {
498+
count_group[key].push(callable_rslt.values);
499+
} else {
500+
count_group[key].push(callable_rslt);
501+
}
502+
503+
}
504+
}
505+
506+
}
507+
}
508+
return this.to_DataFrame(this.key_col, column, count_group, "apply");
509+
}
510+
458511
}

danfojs-browser/tests/core/concat.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
describe("Concatenate", function () {
32

43
it("Check the axis 0 concatenation", function () {
@@ -44,7 +43,7 @@ describe("Concatenate", function () {
4443
assert.deepEqual(new_df.values, data_values);
4544
});
4645

47-
it("concatenate dfd.dataframe and series along 0 axis", function(){
46+
it("concatenate dataframe and series along 0 axis", function(){
4847

4948
let data1 = [ 1, 2, 3, 4 ];
5049
let data2 = [ 3, 4, 5, 6 ];
@@ -73,7 +72,7 @@ describe("Concatenate", function () {
7372

7473
});
7574

76-
it("concatenate dfd.dataframe and series along axis 1", function(){
75+
it("concatenate dataframe and series along axis 1", function(){
7776

7877
let data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
7978
let cols = [ "A", "B", "C" ];

danfojs-browser/tests/core/frame.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ describe("DataFrame", function () {
22762276
let df2 = new dfd.DataFrame([ [ 20, 40, 60, "d" ] ], { "columns": [ "col1", "col2", "col3", "col4" ] });
22772277

22782278
let rslt_df = df.append(df2);
2279-
assert.deepEqual(rslt_df.index, [ 0, 1, 2, 3 ]);
2279+
assert.deepEqual(rslt_df.index, [ "0_row0", "1_row0", "2_row0", "0_row1" ]);
22802280

22812281
});
22822282
});

0 commit comments

Comments
 (0)