Skip to content

Commit aa61093

Browse files
committed
refactor: camelCase and removing duplication
1 parent e0eec27 commit aa61093

1 file changed

Lines changed: 18 additions & 40 deletions

File tree

src/danfojs-base/core/frame.ts

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,60 +1412,38 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
14121412
return this;
14131413
}
14141414

1415-
if (typeof other === "number" && axis === 1) {
1416-
const orig_tensor = this.tensor.clone();
1417-
let unit = [NaN];
1418-
for (let i = 1; i < orig_tensor.shape[orig_tensor.rank - 1]; i++) {
1419-
unit.push(NaN);
1415+
if (typeof other === "number") {
1416+
let origDF = this.copy() as DataFrame;
1417+
if (axis === 0) {
1418+
origDF = origDF.T;
14201419
}
1421-
let pct_array: any[] = orig_tensor.arraySync();
1420+
const originalTensor = origDF.tensor.clone();
1421+
const unit = new Array(originalTensor.shape[originalTensor.rank - 1]).fill(NaN);
1422+
let pctArray: any[] = originalTensor.arraySync();
14221423
if (other > 0) {
14231424
for (let i = 0; i < other; i++) {
1424-
pct_array.unshift(unit);
1425-
pct_array.pop();
1425+
pctArray.unshift(unit);
1426+
pctArray.pop();
14261427
}
14271428
}
14281429
else if (other < 0) {
14291430
for (let i = 0; i > other; i--) {
1430-
pct_array.push(unit);
1431-
pct_array.shift();
1431+
pctArray.push(unit);
1432+
pctArray.shift();
14321433
}
14331434
}
1434-
const pct_tensor = tensorflow.tensor2d(pct_array, orig_tensor.shape);
1435-
const pct_df = (this.$MathOps([orig_tensor, pct_tensor], "divNoNan", inplace) as DataFrame).sub(1);
1436-
return pct_df;
1437-
}
1438-
1439-
if (typeof other === "number" && axis === 0) {
1440-
const orig_df = new DataFrame(this.tensor.clone());
1441-
const orig_tensor = orig_df.T.tensor.clone();
1442-
let unit = [NaN];
1443-
for (let i = 1; i < orig_tensor.shape[orig_tensor.rank - 1]; i++) {
1444-
unit.push(NaN);
1445-
}
1446-
let pct_array: any[] = orig_tensor.arraySync();
1447-
if (other > 0) {
1448-
for (let i = 0; i < other; i++) {
1449-
pct_array.unshift(unit);
1450-
pct_array.pop();
1451-
}
1452-
}
1453-
else if (other < 0) {
1454-
for (let i = 0; i > other; i--) {
1455-
pct_array.push(unit);
1456-
pct_array.shift();
1457-
}
1435+
const pctTensor = tensorflow.tensor2d(pctArray, originalTensor.shape);
1436+
const pctDF = (this.$MathOps([originalTensor, pctTensor], "divNoNan", inplace) as DataFrame).sub(1);
1437+
if (axis === 0) {
1438+
return pctDF.T;
14581439
}
1459-
const pct_tensor = tensorflow.tensor2d(pct_array, orig_tensor.shape);
1460-
const pct_df_flipped = (this.$MathOps([orig_tensor, pct_tensor], "divNoNan", inplace) as DataFrame).sub(1);
1461-
const pct_df = pct_df_flipped.T;
1462-
return pct_df;
1440+
return pctDF;
14631441
}
14641442

14651443
if (other instanceof DataFrame || other instanceof Series) {
14661444
const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
1467-
const pct_df = (this.$MathOps(tensors, "divNoNan", inplace) as DataFrame).sub(1);
1468-
return pct_df;
1445+
const pctDF = (this.$MathOps(tensors, "divNoNan", inplace) as DataFrame).sub(1);
1446+
return pctDF;
14691447
}
14701448
}
14711449

0 commit comments

Comments
 (0)