Skip to content

Commit eda783e

Browse files
committed
implement col for groupby
1 parent 8345e01 commit eda783e

1 file changed

Lines changed: 73 additions & 3 deletions

File tree

src/danfojs-base/aggregators/groupby.ts

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import concat from "../transformers/concat"
1717
export default class Groupby {
1818
colDict?: { [key: string ]: {} }
1919
keyCol: ArrayType1D
20-
data?: ArrayType2D
20+
data?: ArrayType2D | null
2121
columnName: ArrayType1D
2222
colDtype: ArrayType1D
2323
colIndex: ArrayType1D
2424
groupDict?: any
25+
groupColNames?: ArrayType1D
2526

26-
constructor(keyCol: ArrayType1D, data: ArrayType2D, columnName: ArrayType1D, colDtype:ArrayType1D, colIndex: ArrayType1D) {
27+
constructor(keyCol: ArrayType1D, data: ArrayType2D | null, columnName: ArrayType1D, colDtype:ArrayType1D, colIndex: ArrayType1D) {
2728

2829
this.keyCol = keyCol;
2930
this.data = data;
@@ -34,7 +35,7 @@ export default class Groupby {
3435

3536
}
3637

37-
group(): Groupby{
38+
group2(): Groupby{
3839
this.groupDict = this.data?.reduce((prev, current)=>{
3940
function dfs(arr: ArrayType1D, value: ArrayType1D, obj: any) {
4041
let firstIndex = arr[0]
@@ -75,4 +76,73 @@ export default class Groupby {
7576
delete this.data
7677
return this
7778
}
79+
80+
group(): Groupby{
81+
const self = this
82+
const group = this.data?.reduce((prev: any, current)=>{
83+
let indexes= []
84+
for(let i in self.colIndex) {
85+
let index = self.colIndex[i] as number
86+
indexes.push(current[index])
87+
}
88+
let index = indexes.join('-')
89+
90+
if(prev[index]) {
91+
let data = prev[index]
92+
for (let i in self.columnName) {
93+
let colName = self.columnName[i] as string
94+
data[colName].push(current[i])
95+
}
96+
} else {
97+
prev[index] = {}
98+
for (let i in self.columnName) {
99+
let colName = self.columnName[i] as string
100+
prev[index][colName] = [current[i]]
101+
}
102+
}
103+
return prev
104+
105+
}, {})
106+
this.colDict = group
107+
return this
108+
}
109+
110+
col(colNames: ArrayType1D | undefined): Groupby {
111+
type f = {
112+
[key: string]: []
113+
}
114+
if (typeof colNames === "undefined") {
115+
colNames = this.columnName.filter((_, index)=>{
116+
return !this.colIndex.includes(index)
117+
})
118+
}
119+
let self = this
120+
colNames.forEach((val) => {
121+
if (!self.columnName.includes(val))
122+
throw new Error(`Column ${val} does not exist in groups`)
123+
})
124+
let colDict: { [key: string ]: {} } = {...this.colDict}
125+
for(let [key, values] of Object.entries(colDict)) {
126+
let c: { [key: string ]: [] } = {}
127+
let keyVal: any = {...values}
128+
for(let colKey in colNames) {
129+
let colName = colNames[colKey] as string
130+
c[colName] = keyVal[colName]
131+
}
132+
colDict[key] = c
133+
}
134+
const gp = new Groupby(
135+
this.keyCol,
136+
null,
137+
this.columnName,
138+
this.colDtype,
139+
this.colIndex
140+
)
141+
gp.colDict = colDict
142+
gp.groupColNames = colNames
143+
144+
return gp
145+
}
146+
147+
78148
}

0 commit comments

Comments
 (0)