Skip to content

Commit ef28b3e

Browse files
committed
2 parents 898ee66 + 3d45999 commit ef28b3e

3 files changed

Lines changed: 128 additions & 8 deletions

File tree

danfojs/src/core/concat.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DataFrame } from './frame'
22
// import { Series } from "../../src/core/series";
33
import { Utils } from "./utils"
4+
import { Series } from './series'
45

56
const utils = new Utils()
67

@@ -89,9 +90,19 @@ export class Concat {
8990
let val = values[index]
9091
if (typeof data[index] === "undefined") {
9192

92-
data[index] = val;
93+
if(Array.isArray(val)){
94+
data[index] = val
95+
}else{
96+
data[index] = [val];
97+
}
98+
9399
} else {
94-
data[index].push(...val);
100+
if(Array.isArray(val)){
101+
data[index].push(...val);
102+
}else{
103+
data[index].push(val);
104+
}
105+
95106
}
96107
}
97108

@@ -147,7 +158,6 @@ export class Concat {
147158
not_exist.push(col_name);
148159
}
149160
}
150-
151161
if (not_exist.length > 0) {
152162
for (let i = 0; i < value.length; i++) {
153163
let row_value = value[i]
@@ -161,7 +171,12 @@ export class Concat {
161171
new_arr[j] = NaN
162172
} else {
163173
let index = df_columns.indexOf(col_name)
164-
new_arr[j] = row_value[index]
174+
if(Array.isArray(row_value)){
175+
new_arr[j] = row_value[index]
176+
}else{
177+
new_arr[j] = row_value;
178+
}
179+
165180
}
166181

167182
}
@@ -172,9 +187,16 @@ export class Concat {
172187
}
173188

174189
}
190+
191+
if(Array.isArray(data[0])){
192+
let df = new DataFrame(data, { columns: columns });
193+
return df;
194+
}else{
195+
let sf = new Series(data)
196+
return sf
197+
}
175198

176-
let df = new DataFrame(data, { columns: columns });
177-
return df;
199+
178200

179201
}
180202

danfojs/src/core/groupby.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ export class GroupBy {
306306
return df
307307
}
308308

309+
max(){
310+
let value = this.arithemetic("max()")
311+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"max")
312+
return df
313+
}
314+
315+
min(){
316+
let value = this.arithemetic("min()")
317+
let df = this.to_DataFrame(this.key_col, this.group_col_name,value,"min")
318+
return df
319+
}
320+
309321
/**
310322
* returns dataframe of a group
311323
* @param {*} key [Array]
@@ -366,7 +378,7 @@ export class GroupBy {
366378
let k_data = key_val[key_2]
367379
let kk = []
368380
kk[0] = isNaN(parseInt(key_1)) ? key_1 : parseInt(key_1)
369-
kk[1] = isNaN(parseInt(key_2)) ? key_1 : parseInt(key_2)
381+
kk[1] = isNaN(parseInt(key_2)) ? key_2 : parseInt(key_2)
370382
kk.push(...k_data)
371383
df_data.push(kk)
372384

@@ -396,7 +408,7 @@ export class GroupBy {
396408
}
397409
let column = [...key_col]
398410
let group_col = col.slice().map((x,i)=>{
399-
if(ops.length >1){
411+
if(Array.isArray(ops)){
400412
return `${x}_${ops[i]}`
401413
}
402414
return `${x}_${ops}`

danfojs/tests/core/concat.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert } from "chai"
22
import { DataFrame } from '../../src/core/frame'
33
import {concat} from '../../src/core/concat'
4+
import { Series } from "../../src/core/series";
45

56

67

@@ -48,4 +49,89 @@ describe("Concatenate", function () {
4849
NaN, NaN, NaN]]
4950
assert.deepEqual(new_df.values, data_values);
5051
});
52+
53+
it("concatenate dataframe and series along 0 axis",function(){
54+
55+
let data1 = [1,2,3,4]
56+
let data2 = [3,4,5,6]
57+
58+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
59+
let cols = ["A", "B", "C"]
60+
let df = new DataFrame(data, { columns: cols })
61+
62+
let s1 = new Series(data1)
63+
let s2 = new Series(data2)
64+
let rslt = [
65+
[ 1, 2, 3, NaN ],
66+
[ 4, 5, 6, NaN ],
67+
[ 20, 30, 40, NaN ],
68+
[ 39, 89, 78, NaN ],
69+
[ NaN, NaN, NaN, 1 ],
70+
[ NaN, NaN, NaN, 2 ],
71+
[ NaN, NaN, NaN, 3 ],
72+
[ NaN, NaN, NaN, 4 ]
73+
]
74+
75+
76+
let con = concat({ "df_list": [df,s1], "axis": 0 })
77+
78+
assert.deepEqual(con.values, rslt)
79+
80+
})
81+
82+
it("concatenate dataframe and series along axis 1",function(){
83+
84+
let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
85+
let cols = ["A", "B", "C"]
86+
let df = new DataFrame(data, { columns: cols })
87+
88+
let data1 = [1,2,3,4]
89+
let s1 = new Series(data1)
90+
let rslt = [
91+
[ 1, 2, 3, 1 ],
92+
[ 4, 5, 6, 2 ],
93+
[ 20, 30, 40, 3 ],
94+
[ 39, 89, 78, 4 ]
95+
]
96+
97+
98+
let con = concat({ "df_list": [df,s1], "axis": 1 })
99+
100+
assert.deepEqual(con.values, rslt)
101+
102+
})
103+
it("concatenate series along axis 1",function(){
104+
105+
let data1 = [1,2,3,4]
106+
let data2 = [3,4,5,6]
107+
108+
let s1 = new Series(data1)
109+
let s2 = new Series(data2)
110+
let rslt = [ [ 1, 3 ], [ 2, 4 ], [ 3, 5 ], [ 4, 6 ] ]
111+
112+
113+
let con = concat({ "df_list": [s1,s2], "axis": 1 })
114+
115+
assert.deepEqual(con.values, rslt)
116+
117+
})
118+
it("concatenate series along axis 0",function(){
119+
120+
let data1 = [1,2,3,4]
121+
let data2 = [3,4,5,6]
122+
123+
let s1 = new Series(data1)
124+
let s2 = new Series(data2)
125+
let rslt = [
126+
1, 2, 3, 4,
127+
3, 4, 5, 6
128+
]
129+
130+
let con = concat({ "df_list": [s1,s2], "axis": 0 })
131+
132+
assert.deepEqual(con.values, rslt)
133+
134+
})
135+
136+
51137
});

0 commit comments

Comments
 (0)