Skip to content

Commit 6364063

Browse files
committed
add iloc to series
1 parent 2e14feb commit 6364063

4 files changed

Lines changed: 73 additions & 28 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,9 +2021,8 @@ export class DataFrame extends Ndframe {
20212021
if (!utils.__key_in_object(kwargs, "mapper")) {
20222022
throw Error("Please specify a mapper object")
20232023
}
2024-
this.print()
2025-
console.log(kwargs['axis']);
2026-
console.log(kwargs['inplace']);
2024+
// console.log(kwargs['axis']);
2025+
// console.log(kwargs['inplace']);
20272026
if (kwargs['axis'] == 1) {
20282027
//columns
20292028
let old_col_names = Object.keys(kwargs['mapper'])
@@ -2039,19 +2038,15 @@ export class DataFrame extends Ndframe {
20392038
col_names[idx] = new_col_names[i]
20402039

20412040
})
2042-
console.log("Before rename");
2043-
this.print()
2041+
20442042
if (kwargs['inplace']) {
20452043
this.columns = col_names
20462044
this.__set_col_property(this, this.col_data, col_names, old_col_names)
2047-
console.log("after rename inplace");
2048-
this.print()
2045+
20492046
} else {
20502047
let df = this.copy()
20512048
df.columns = col_names
20522049
df.__set_col_property(df, df.col_data, col_names, old_col_names)
2053-
console.log("after rename not inplace");
2054-
df.print()
20552050
return df
20562051
}
20572052
} else {

danfojs/src/core/indexing.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,39 @@ export const indexLoc = (ndframe, kwargs) => {
136136
throw new Error(`Specified row index ${row_val} is bigger than maximum row index of ${max_rowIndex}`);
137137
}
138138

139-
let value = data_values[row_val]
140-
let row_data = []
139+
if(Array.isArray(data_values[0])){
141140

142-
for (var i in columns) {
143-
var col_index;
144-
if (kwargs["type"] == "loc" && !isColumnSplit) {
145-
col_index = ndframe.columns.indexOf(columns[i]); //obtain the column index
141+
let value = data_values[row_val]
142+
let row_data = []
146143

147-
if (col_index == -1) {
148-
throw new Error(`Column ${columns[i]} does not exist`);
149-
}
150-
} else {
151-
col_index = columns[i];
152-
let max_colIndex = ndframe.columns.length - 1; //assign the maximum column index to a value
153144

154-
if (col_index > max_colIndex) {
155-
throw new Error(`column index ${col_index} is bigger than ${max_colIndex}`);
145+
for (var i in columns) {
146+
var col_index;
147+
if (kwargs["type"] == "loc" && !isColumnSplit) {
148+
col_index = ndframe.columns.indexOf(columns[i]); //obtain the column index
149+
150+
if (col_index == -1) {
151+
throw new Error(`Column ${columns[i]} does not exist`);
152+
}
153+
} else {
154+
col_index = columns[i];
155+
let max_colIndex = ndframe.columns.length - 1; //assign the maximum column index to a value
156+
157+
if (col_index > max_colIndex) {
158+
throw new Error(`column index ${col_index} is bigger than ${max_colIndex}`);
159+
}
156160
}
161+
162+
let elem = value[col_index]; //obtain the element at the column index
163+
row_data.push(elem);
157164
}
158165

159-
let elem = value[col_index]; //obtain the element at the column index
160-
row_data.push(elem);
161-
}
166+
new_data.push(row_data); //store the data for each row in the new_data
162167

163-
new_data.push(row_data); //store the data for each row in the new_data
168+
}else{
169+
170+
new_data.push(data_values[row_val])
171+
}
164172

165173
}
166174

danfojs/src/core/series.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { table } from 'table'
88
import { Configs } from '../config/config'
99
import { TimeSeries } from './timeseries';
1010
import { Plot } from '../plotting/plot'
11-
11+
import { indexLoc } from '../core/indexing'
1212

1313

1414
const utils = new Utils()
@@ -1307,5 +1307,24 @@ export class Series extends NDframe {
13071307
return plt
13081308
}
13091309

1310+
/**
1311+
* Slice series accross the index
1312+
* @param {*} row --> Array
1313+
* @returns Series
1314+
*/
1315+
iloc(row){
1316+
1317+
let kwargs = {}
1318+
1319+
kwargs["rows"] = row
1320+
kwargs["type"] = "iloc"
1321+
1322+
let [new_data, columns, rows] = indexLoc(this, kwargs);
1323+
1324+
let sf = new Series(new_data, {columns: columns});
1325+
sf.__set_index(rows)
1326+
1327+
return sf
1328+
}
13101329
}
13111330

danfojs/tests/core/series.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,27 @@ describe("Series", function () {
11481148

11491149

11501150
})
1151+
1152+
describe("iloc", function(){
1153+
1154+
it("indexing by list of index",function(){
1155+
let data = [1,2,3,4,"a","b","c"]
1156+
let sf = new Series(data)
1157+
1158+
let expected_val = [ 2, 'a', 3, 4, 'b' ]
1159+
1160+
assert.deepEqual(sf.iloc([1,4,2,3,5]).values, expected_val)
1161+
1162+
});
1163+
it("indexing by slicing", function(){
1164+
1165+
let data = [1,2,3,4,"a","b","c"]
1166+
let sf = new Series(data)
1167+
1168+
let expected_val = [ 2, 3, 4 ]
1169+
1170+
assert.deepEqual(sf.iloc(["1:4"]).values, expected_val)
1171+
1172+
});
1173+
});
11511174
})

0 commit comments

Comments
 (0)