Skip to content

Commit f49f16c

Browse files
authored
Merge pull request #418 from NeonSpork/pct_change-port
feat: Added `pctChange()` to DataFrame and `divNoNan()` to `$MathOps` in DataFrame
2 parents b712f70 + a6d0372 commit f49f16c

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
329329
case 'div':
330330
tensorResult = tensors[0].div(tensors[1])
331331
break;
332+
case 'divNoNan':
333+
tensorResult = tensors[0].divNoNan(tensors[1])
334+
break;
332335
case 'mul':
333336
tensorResult = tensors[0].mul(tensors[1])
334337
break;
@@ -726,6 +729,36 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
726729
return this.$MathOps(tensors, "div", inplace)
727730

728731

732+
}
733+
734+
/**
735+
* Return division of DataFrame with other, returns 0 if denominator is 0.
736+
* @param other DataFrame, Series, Array or Scalar number to divide with.
737+
* @param options.axis 0 or 1. If 0, compute the division column-wise, if 1, row-wise
738+
* @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
739+
* @example
740+
* ```
741+
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] })
742+
* const df2 = df.divNoNan(2)
743+
* df2.print()
744+
* ```
745+
*/
746+
divNoNan(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame
747+
divNoNan(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void {
748+
const { inplace, axis } = { inplace: false, axis: 1, ...options }
749+
750+
if (this.$frameIsNotCompactibleForArithmeticOperation()) {
751+
throw Error("TypeError: div operation is not supported for string dtypes");
752+
}
753+
754+
if ([0, 1].indexOf(axis) === -1) {
755+
throw Error("ParamError: Axis must be 0 or 1");
756+
}
757+
758+
const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
759+
return this.$MathOps(tensors, "divNoNan", inplace)
760+
761+
729762
}
730763

731764
/**
@@ -1333,6 +1366,87 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
13331366

13341367
}
13351368

1369+
/**
1370+
* Return percentage difference of DataFrame with other.
1371+
* @param other DataFrame, Series, Array or Scalar number (positive numbers are preceding rows, negative are following rows) to compare difference with.
1372+
* @param options.axis 0 or 1. If 0, compute the difference column-wise, if 1, row-wise
1373+
* @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
1374+
* @example
1375+
* ```
1376+
* const df = new DataFrame([[1, 2, 3, 4, 5, 6], [1, 1, 2, 3, 5, 8], [1, 4, 9, 16, 25, 36]], { columns: ['A', 'B', 'C'] })
1377+
*
1378+
* // Percentage difference with previous row
1379+
* const df0 = df.pctChange(1)
1380+
* console.log(df0)
1381+
*
1382+
* // Percentage difference with previous column
1383+
* const df1 = df.pctChange(1, {axis: 0})
1384+
* console.log(df1)
1385+
*
1386+
* // Percentage difference with previous 3rd previous row
1387+
* const df2 = df.pctChange(3)
1388+
* console.log(df2)
1389+
*
1390+
* // Percentage difference with following row
1391+
* const df3 = df.pctChange(-1)
1392+
* console.log(df3)
1393+
*
1394+
* // Percentage difference with another DataFrame
1395+
* const df4 = df.pctChange(df3)
1396+
* console.log(df4)
1397+
* ```
1398+
*/
1399+
pctChange(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame
1400+
pctChange(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void {
1401+
const { inplace, axis } = { inplace: false, axis: 1, ...options }
1402+
1403+
if (this.$frameIsNotCompactibleForArithmeticOperation()) {
1404+
throw Error("TypeError: pctChange operation is not supported for string dtypes");
1405+
}
1406+
1407+
if ([0, 1].indexOf(axis) === -1) {
1408+
throw Error("ParamError: Axis must be 0 or 1");
1409+
}
1410+
1411+
if (other === 0) {
1412+
return this;
1413+
}
1414+
1415+
if (typeof other === "number") {
1416+
let origDF = this.copy() as DataFrame;
1417+
if (axis === 0) {
1418+
origDF = origDF.T;
1419+
}
1420+
const originalTensor = origDF.tensor.clone();
1421+
const unit = new Array(originalTensor.shape[originalTensor.rank - 1]).fill(NaN);
1422+
let pctArray: any[] = originalTensor.arraySync();
1423+
if (other > 0) {
1424+
for (let i = 0; i < other; i++) {
1425+
pctArray.unshift(unit);
1426+
pctArray.pop();
1427+
}
1428+
}
1429+
else if (other < 0) {
1430+
for (let i = 0; i > other; i--) {
1431+
pctArray.push(unit);
1432+
pctArray.shift();
1433+
}
1434+
}
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;
1439+
}
1440+
return pctDF;
1441+
}
1442+
1443+
if (other instanceof DataFrame || other instanceof Series) {
1444+
const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
1445+
const pctDF = (this.$MathOps(tensors, "divNoNan", inplace) as DataFrame).sub(1);
1446+
return pctDF;
1447+
}
1448+
}
1449+
13361450
/**
13371451
* Return difference of DataFrame with other.
13381452
* @param other DataFrame, Series, Array or Scalar number (positive numbers are preceding rows, negative are following rows) to compare difference with.

src/danfojs-base/shared/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ export interface DataFrameInterface extends NDframeInterface {
216216
sub(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
217217
mul(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
218218
div(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
219+
divNoNan(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
219220
pow(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
220221
mod(other: DataFrame | Series | number | number[], options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
222+
pctChange(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
221223
diff(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void
222224
mean(options?: { axis?: 0 | 1 }): Series
223225
median(options?: { axis?: 0 | 1 }): Series

src/danfojs-browser/tests/core/frame.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,51 @@ describe("DataFrame", function () {
10181018

10191019
});
10201020

1021+
describe("pctChange", function () {
1022+
it("Return same DataFrame if other === 0", function () {
1023+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1024+
const df = new dfd.DataFrame(data);
1025+
assert.deepEqual((df.pctChange(0)).values, [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ]);
1026+
});
1027+
it("Return difference in percentage of DataFrame with previous row", function () {
1028+
const data = [ [ 90 ], [ 900 ], [ 900 ] ];
1029+
const df = new dfd.DataFrame(data);
1030+
assert.deepEqual((df.pctChange(1)).values, [ [ NaN ], [ 9 ], [ 0 ] ]);
1031+
});
1032+
it("Return difference in percentage of DataFrame with following row", function () {
1033+
const data = [ [ 0, 5, 15 ], [ 10, 10, 10 ], [ 1, 2, 5 ] ];
1034+
const df = new dfd.DataFrame(data);
1035+
assert.deepEqual((df.pctChange(-1)).values, [ [ -1, -0.5, 0.5 ], [ 9, 4, 1 ], [ NaN, NaN, NaN ] ]);
1036+
});
1037+
it("Return difference in percentage of a DataFrame with a Series along default axis 1", function () {
1038+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1039+
const sf = new dfd.Series([ 1, 2, 1 ]);
1040+
const df = new dfd.DataFrame(data);
1041+
assert.deepEqual((df.pctChange(sf)).values, [ [ -1, 0, 3 ], [ 9, 4, 9 ], [ 0, 0, 2 ] ]);
1042+
});
1043+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), previous column", function () {
1044+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];
1045+
const df = new dfd.DataFrame(data);
1046+
assert.deepEqual((df.pctChange(1, { axis: 0 })).values, [ [ NaN, -1, 1 ], [ NaN, 0, 0 ], [ NaN, 1, 0.5 ] ]);
1047+
});
1048+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), following column", function () {
1049+
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 8 ] ];
1050+
const df = new dfd.DataFrame(data);
1051+
assert.deepEqual((df.pctChange(-1, { axis: 0 })).values, [ [ -1, -0.5, NaN ], [ 0, 0, NaN ], [ -0.5, -0.75, NaN ] ]);
1052+
});
1053+
it("Return difference in percentage of a DataFrame with another DataFrame along default axis 1", function () {
1054+
const df1 = new dfd.DataFrame([ [ 0, 2, 4 ], [ 3, 10, 4 ] ]);
1055+
const df2 = new dfd.DataFrame([ [ -1, -2, 4 ], [ 6, 5, 0 ] ]);
1056+
assert.deepEqual((df1.pctChange(df2)).values, [ [ -1, -2, 0 ], [ -0.5, 1, -1 ] ]);
1057+
});
1058+
it("Throw error if DataFrame for pctChange contains string", function () {
1059+
const df = new dfd.DataFrame([ [ "words", "words", "words" ], [ "words", "words", "words" ] ]);
1060+
assert.throws(() => {
1061+
df.pctChange(1);
1062+
}, Error, "TypeError: pctChange operation is not supported for string dtypes");
1063+
});
1064+
});
1065+
10211066
describe("diff", function () {
10221067
it("Return same DataFrame if other === 0", function () {
10231068
const data = [ [ 0, 2, 4 ], [ 10, 10, 10 ], [ 1, 2, 3 ] ];

src/danfojs-node/test/core/frame.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,51 @@ describe("DataFrame", function () {
10371037
});
10381038
});
10391039

1040+
describe("pctChange", function () {
1041+
it("Return same DataFrame if other === 0", function () {
1042+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1043+
const df = new DataFrame(data);
1044+
assert.deepEqual((df.pctChange(0) as DataFrame).values, [[0, 2, 4], [10, 10, 10], [1, 2, 3]]);
1045+
});
1046+
it("Return difference in percentage of DataFrame with previous row", function () {
1047+
const data = [[90], [900], [900]];
1048+
const df = new DataFrame(data);
1049+
assert.deepEqual((df.pctChange(1) as DataFrame).values, [[NaN], [9], [0]]);
1050+
});
1051+
it("Return difference in percentage of DataFrame with following row", function () {
1052+
const data = [[0, 5, 15], [10, 10, 10], [1, 2, 5]];
1053+
const df = new DataFrame(data);
1054+
assert.deepEqual((df.pctChange(-1) as DataFrame).values, [[-1, -0.5, 0.5], [9, 4, 1], [NaN, NaN, NaN]]);
1055+
});
1056+
it("Return difference in percentage of a DataFrame with a Series along default axis 1", function () {
1057+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1058+
const sf = new Series([1, 2, 1]);
1059+
const df = new DataFrame(data);
1060+
assert.deepEqual((df.pctChange(sf) as DataFrame).values, [[-1, 0, 3], [9, 4, 9], [0, 0, 2]]);
1061+
});
1062+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), previous column", function () {
1063+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 3]];
1064+
const df = new DataFrame(data);
1065+
assert.deepEqual((df.pctChange(1, { axis: 0 }) as DataFrame).values, [[NaN, -1, 1], [NaN, 0, 0], [NaN, 1, 0.5]]);
1066+
});
1067+
it("Return difference in percentage of a DataFrame with along axis 0 (column-wise), following column", function () {
1068+
const data = [[0, 2, 4], [10, 10, 10], [1, 2, 8]];
1069+
const df = new DataFrame(data);
1070+
assert.deepEqual((df.pctChange(-1, { axis: 0 }) as DataFrame).values, [[-1, -0.5, NaN], [0, 0, NaN], [-0.5, -0.75, NaN]]);
1071+
});
1072+
it("Return difference in percentage of a DataFrame with another DataFrame along default axis 1", function () {
1073+
const df1 = new DataFrame([[0, 2, 4], [3, 10, 4]]);
1074+
const df2 = new DataFrame([[-1, -2, 4], [6, 5, 0]]);
1075+
assert.deepEqual((df1.pctChange(df2) as DataFrame).values, [[-1, -2, 0], [-0.5, 1, -1]]);
1076+
});
1077+
it("Throw error if DataFrame for pctChange contains string", function () {
1078+
const df = new DataFrame([["words", "words", "words"], ["words", "words", "words"]]);
1079+
assert.throws(() => {
1080+
df.pctChange(1);
1081+
}, Error, "TypeError: pctChange operation is not supported for string dtypes");
1082+
});
1083+
});
1084+
10401085
describe("mod", function () {
10411086
it("Return modulus of DataFrame with a single Number", function () {
10421087
const data = [[0, 2, 4], [360, 180, 360]];

0 commit comments

Comments
 (0)