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

Method pctChange

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

Source from the content-addressed store, hash-verified

1391 */
1392 pctChange(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame
1393 pctChange(other: DataFrame | Series | number[] | number, options?: { axis?: 0 | 1, inplace?: boolean }): DataFrame | void {
1394 const { inplace, axis } = { inplace: false, axis: 1, ...options }
1395
1396 if (this.$frameIsNotCompactibleForArithmeticOperation()) {
1397 throw Error("TypeError: pctChange operation is not supported for string dtypes");
1398 }
1399
1400 if ([0, 1].indexOf(axis) === -1) {
1401 throw Error("ParamError: Axis must be 0 or 1");
1402 }
1403
1404 if (other === 0) {
1405 return this;
1406 }
1407
1408 if (typeof other === "number") {
1409 let origDF = this.copy() as DataFrame;
1410 if (axis === 0) {
1411 origDF = origDF.T;
1412 }
1413 const originalTensor = origDF.tensor.clone();
1414 const unit = new Array(originalTensor.shape[originalTensor.rank - 1]).fill(NaN);
1415 let pctArray: any[] = originalTensor.arraySync();
1416 if (other > 0) {
1417 for (let i = 0; i < other; i++) {
1418 pctArray.unshift(unit);
1419 pctArray.pop();
1420 }
1421 }
1422 else if (other < 0) {
1423 for (let i = 0; i > other; i--) {
1424 pctArray.push(unit);
1425 pctArray.shift();
1426 }
1427 }
1428 const pctTensor = tensorflow.tensor2d(pctArray, originalTensor.shape);
1429 const pctDF = (this.$MathOps([originalTensor, pctTensor], "divNoNan", inplace) as DataFrame).sub(1);
1430 if (axis === 0) {
1431 return pctDF.T;
1432 }
1433 return pctDF;
1434 }
1435
1436 if (other instanceof DataFrame || other instanceof Series) {
1437 const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
1438 const pctDF = (this.$MathOps(tensors, "divNoNan", inplace) as DataFrame).sub(1);
1439 return pctDF;
1440 }
1441 }
1442
1443 /**
1444 * Return difference of DataFrame with other.

Callers

nothing calls this directly

Calls 6

copyMethod · 0.95
$MathOpsMethod · 0.95
indexOfMethod · 0.80
subMethod · 0.65

Tested by

no test coverage detected