MCPcopy Create free account
hub / github.com/javascriptdata/danfojs / dropDuplicates

Method dropDuplicates

src/danfojs-base/core/series.ts:1697–1740  ·  view source on GitHub ↗
(options?: { keep?: "first" | "last", inplace?: boolean })

Source from the content-addressed store, hash-verified

1695 */
1696 dropDuplicates(options?: { keep?: "first" | "last", inplace?: boolean }): Series
1697 dropDuplicates(options?: { keep?: "first" | "last", inplace?: boolean }): Series | void {
1698 const { keep, inplace } = { keep: "first", inplace: false, ...options }
1699
1700 if (!(["first", "last"].includes(keep))) {
1701 throw Error(`Params Error: Keep must be one of 'first' or 'last'`);
1702 }
1703
1704 let dataArr: ArrayType1D
1705 let newArr: ArrayType1D = [];
1706 let oldIndex: Array<string | number>
1707 let newIndex: Array<string | number> = [];
1708
1709 if (keep === "last") {
1710 dataArr = (this.values as ArrayType1D).reverse();
1711 oldIndex = this.index.reverse();
1712 } else {
1713 dataArr = (this.values as ArrayType1D)
1714 oldIndex = this.index;
1715 }
1716
1717 dataArr.forEach((val, i) => {
1718 if (!newArr.includes(val)) {
1719 newIndex.push(oldIndex[i]);
1720 newArr.push(val);
1721 }
1722 });
1723
1724 if (keep === "last") {
1725 //re-reversed the array and index to its true order
1726 newArr = newArr.reverse();
1727 newIndex = newIndex.reverse();
1728 }
1729
1730 if (inplace) {
1731 this.$setValues(newArr, false)
1732 this.$setIndex(newIndex)
1733 } else {
1734 const sf = this.copy();
1735 sf.$setValues(newArr, false)
1736 sf.$setIndex(newIndex)
1737 return sf;
1738 }
1739
1740 }
1741
1742 /**
1743 * Cast Series to specified data type

Callers

nothing calls this directly

Calls 4

copyMethod · 0.95
includesMethod · 0.80
$setValuesMethod · 0.80
$setIndexMethod · 0.65

Tested by

no test coverage detected