MCPcopy Index your code
hub / github.com/javascriptdata/danfojs / diff

Method diff

src/danfojs-base/core/frame.ts:1474–1521  ·  view source on GitHub ↗
(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean })

Source from the content-addressed store, hash-verified

1472 */
1473 diff(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame
1474 diff(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void {
1475 const { inplace, axis } = { inplace: false, axis: 1, ...options }
1476
1477 if (this.$frameIsNotCompactibleForArithmeticOperation()) {
1478 throw Error("TypeError: diff operation is not supported for string dtypes");
1479 }
1480
1481 if ([0, 1].indexOf(axis) === -1) {
1482 throw Error("ParamError: Axis must be 0 or 1");
1483 }
1484
1485 if (other === 0) {
1486 return this;
1487 }
1488
1489 if (typeof other === "number") {
1490 let origDF = this.copy() as DataFrame;
1491 if (axis === 0) {
1492 origDF = origDF.T;
1493 }
1494 const originalTensor = origDF.tensor.clone();
1495 const unit = new Array(originalTensor.shape[originalTensor.rank - 1]).fill(NaN);
1496 let diffArray: any[] = originalTensor.arraySync();
1497 if (other > 0) {
1498 for (let i = 0; i < other; i++) {
1499 diffArray.unshift(unit);
1500 diffArray.pop();
1501 }
1502 }
1503 else if (other < 0) {
1504 for (let i = 0; i > other; i--) {
1505 diffArray.push(unit);
1506 diffArray.shift();
1507 }
1508 }
1509 const diffTensor = tensorflow.tensor2d(diffArray, originalTensor.shape);
1510 const diffDF = this.$MathOps([originalTensor, diffTensor], "sub", inplace) as DataFrame;
1511 if (axis === 0) {
1512 return diffDF.T;
1513 }
1514 return diffDF;
1515 }
1516
1517 if (other instanceof DataFrame || other instanceof Series) {
1518 const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
1519 return this.$MathOps(tensors, "sub", inplace);
1520 }
1521 }
1522
1523 /**
1524 * Return the absolute value of elements in a DataFrame.

Callers

nothing calls this directly

Calls 5

copyMethod · 0.95
$MathOpsMethod · 0.95
indexOfMethod · 0.80

Tested by

no test coverage detected