Skip to content

Commit afc41dd

Browse files
committed
2 parents efc48c1 + a655247 commit afc41dd

8 files changed

Lines changed: 428 additions & 113 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
## danfojs: powerful javascript data analysis toolkit
99
![Node.js CI](https://github.com/opensource9ja/danfojs/workflows/Node.js%20CI/badge.svg?branch=master)
10+
[![](https://data.jsdelivr.com/v1/package/npm/danfojs/badge?style=rounded)](https://www.jsdelivr.com/package/npm/danfojs)
11+
1012

1113

1214
## What is it?
@@ -179,9 +181,6 @@ To use danfo.js via script tags, copy and paste the CDN below to your HTML file
179181
</html>
180182
```
181183

182-
### To install Node.js version, go [here](https://www.npmjs.com/package/danfojs-node)
183-
184-
185184
## Installation from sources
186185
To install danfo in [development mode], clone the repo:
187186

danfojs/src/core/frame.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Series } from "./series"
44
import * as tf from '@tensorflow/tfjs'
55
import { Utils } from "./utils"
66
import { GroupBy } from "./groupby"
7-
import { Plot } from '../plotting/plot'
7+
// import { Plot } from '../plotting/plot'
88
import { indexLoc } from '../core/indexing'
99

1010
const utils = new Utils
@@ -1895,10 +1895,10 @@ export class DataFrame extends Ndframe {
18951895
* @param {string} div Name of the div to show the plot
18961896
* @param {Object} config configuration options for making Plots, supports Plotly parameters
18971897
*/
1898-
plot(div, config = {}) {
1899-
const plt = new Plot()
1900-
plt.plot(this, div, config)
1901-
}
1898+
// plot(div, config = {}) {
1899+
// const plt = new Plot()
1900+
// plt.plot(this, div, config)
1901+
// }
19021902

19031903

19041904
/**

danfojs/src/core/groupby.js

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class GroupBy {
120120
throw new Error(`Col_name must be an array of column`)
121121
}
122122

123-
123+
this.group_col_name = col_names // store the column name
124124
if(this.key_col.length ==2){
125125

126126
this.group_col = {}
@@ -165,13 +165,20 @@ export class GroupBy {
165165
*/
166166
arithemetic(operation){
167167

168-
let ops_name = ["mean","sum","count","mode"]
168+
let ops_name = ["mean","sum","count","mode","std","var","cumsum","cumprod",
169+
"cummax","cummin"]
169170

170171
let ops_map = {
171172
"mean": "mean()",
172173
"sum": "sum()",
173174
"mode": "mode()",
174-
"count": "count()"
175+
"count": "count()",
176+
"std" : "std()",
177+
"var" : "var()",
178+
"cumsum" : "cumsum().values",
179+
"cumprod": "cumprod().values",
180+
"cummax" : "cummax().values",
181+
"cummin" : "cummin().values"
175182
}
176183
let is_array = false;
177184

@@ -201,8 +208,11 @@ export class GroupBy {
201208
}else{
202209
data = eval(`this.group_col[key1][key2][i].${operation}`)
203210
}
204-
205-
count_group[key1][key2].push(data)
211+
if(Array.isArray(data)){
212+
count_group[key1][key2].push(...data)
213+
}else{
214+
count_group[key1][key2].push(data)
215+
}
206216
}
207217

208218
}
@@ -227,8 +237,12 @@ export class GroupBy {
227237
}else{
228238
data = eval(`this.group_col[key1][i].${operation}`)
229239
}
240+
if(Array.isArray(data)){
241+
count_group[key1].push(...data)
242+
}else{
243+
count_group[key1].push(data)
244+
}
230245

231-
count_group[key1].push(data)
232246
}
233247
}
234248

@@ -241,15 +255,56 @@ export class GroupBy {
241255
count(){
242256

243257
let value = this.arithemetic("count()");
244-
return value;
258+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"count")
259+
return df
245260
}
246261

247262
sum(){
248263
let value = this.arithemetic("sum()")
249-
return value
264+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"sum")
265+
return df
266+
}
267+
268+
std(){
269+
let value = this.arithemetic("std()")
270+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"std")
271+
return df
272+
}
273+
274+
var(){
275+
let value = this.arithemetic("var()")
276+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"var")
277+
return df
250278
}
251279

280+
mean(){
281+
let value = this.arithemetic("mean()")
282+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"mean")
283+
return df
284+
}
252285

286+
cumsum(){
287+
let value = this.arithemetic("cumsum().values")
288+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"cumsum")
289+
return df
290+
}
291+
cummax(){
292+
let value = this.arithemetic("cummax().values")
293+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"cummax")
294+
return df
295+
}
296+
297+
cumprod(){
298+
let value = this.arithemetic("cumprod().values")
299+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"cumprod")
300+
return df
301+
}
302+
303+
cummin(){
304+
let value = this.arithemetic("cummin().values")
305+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"cummin")
306+
return df
307+
}
253308

254309
/**
255310
* returns dataframe of a group
@@ -293,8 +348,63 @@ export class GroupBy {
293348
this.col(columns)
294349

295350
let data = this.arithemetic(operations)
351+
let df = this.to_DataFrame(this.key_col,this.group_col_name,data,operations)
352+
353+
return df;
354+
}
296355

297-
return data;
356+
to_DataFrame(key_col,col,data,ops){
357+
358+
if(key_col.length ==2){
359+
360+
let df_data = []
361+
for(let key_1 in data){
362+
363+
let key_val = data[key_1]
364+
365+
for(let key_2 in key_val){
366+
let k_data = key_val[key_2]
367+
let kk = []
368+
kk[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
369+
kk[1] = isNaN(parseInt(key_2)) ? key_1 : parseInt(key_2)
370+
kk.push(...k_data)
371+
df_data.push(kk)
372+
373+
}
374+
375+
}
376+
let column = [...key_col]
377+
378+
let group_col = col.slice().map((x,i)=>{
379+
if(Array.isArray(ops)){
380+
return `${x}_${ops[i]}`
381+
}
382+
return `${x}_${ops}`
383+
});
384+
column.push(...group_col);
385+
return new DataFrame(df_data,{columns: column})
386+
}else{
387+
let df_data = []
388+
for(let key_1 in data){
389+
390+
let key_val = data[key_1]
391+
392+
let key_data = []
393+
key_data[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
394+
key_data.push(...key_val)
395+
df_data.push(key_data)
396+
}
397+
let column = [...key_col]
398+
let group_col = col.slice().map((x,i)=>{
399+
if(ops.length >1){
400+
return `${x}_${ops[i]}`
401+
}
402+
return `${x}_${ops}`
403+
});
404+
column.push(...group_col);
405+
406+
return new DataFrame(df_data,{columns: column})
407+
}
298408
}
299409

300410
}

danfojs/src/core/series.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ export class Series extends NDframe {
106106
let new_values = []
107107
let new_idx = []
108108

109-
let counts = [...Array(idx.length).keys()] //set index
109+
// let counts = [...Array(idx.length).keys()] //set index
110110
//get random sampled numbers
111-
let rand_nums = utils.__randgen(num, 0, counts.length)
111+
// let index_arr = utils.__range(0,counts.length)
112+
let rand_nums = utils.__shuffle(num,idx)
113+
// console.log(rand_nums)
112114
rand_nums.map(i => {
113115
new_values.push(values[i])
114116
new_idx.push(idx[i])

danfojs/src/core/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,26 @@ export class Utils {
637637
}
638638
return zero_data
639639
}
640+
641+
__shuffle(num,array) {
642+
//https://stackoverflow.com/questions/18806210/generating-non-repeating-random-numbers-in-js/18806417
643+
var i = array.length,
644+
j = 0,
645+
temp;
646+
647+
while (i--) {
648+
649+
j = Math.floor(Math.random() * (i+1));
650+
651+
// swap randomly chosen element with current element
652+
temp = array[i];
653+
array[i] = array[j];
654+
array[j] = temp;
655+
656+
}
657+
658+
return array.slice(0,num);
659+
}
640660
}
641661

642662

0 commit comments

Comments
 (0)