Skip to content

Commit

Permalink
Update index property after logical operations on Series
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Oct 3, 2021
1 parent 771f8c9 commit a5ba784
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 165 deletions.
45 changes: 17 additions & 28 deletions danfojs-browser/src/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,11 @@ export default class Series extends NDframe {
break;
}
}
return new Series(data);

return new Series(data, {
index: this.index,
config: { ...this.config }
});

}

Expand Down Expand Up @@ -1249,6 +1253,7 @@ export default class Series extends NDframe {
if (other === undefined) {
throw new Error("Param Error: other cannot be undefined");
}
const newValues = [];

if (other instanceof Series) {
if (this.dtypes[0] !== other.dtypes[0]) {
Expand All @@ -1258,37 +1263,28 @@ export default class Series extends NDframe {
if (this.shape[0] !== other.shape[0]) {
throw new Error("Param Error must be of same shape");
}

const newValues = [];

this.values.forEach((val, i) => {
newValues.push(Boolean(val) && Boolean(other.values[i]));
});

return new Series(newValues, {
config: { ...this.config }
});
} else if (Array.isArray(other)) {
const newValues = [];

this.values.forEach((val, i) => {
newValues.push(Boolean(val) && Boolean(other[i]));
});

return new Series(newValues, {
config: { ...this.config }
});
} else {
const newValues = [];

this.values.forEach((val) => {
newValues.push(Boolean(val) && Boolean(other));
});

return new Series(newValues, {
config: { ...this.config }
});
}

return new Series(newValues, {
index: this.index,
config: { ...this.config }
});
}

/**
Expand All @@ -1300,6 +1296,7 @@ export default class Series extends NDframe {
if (other === undefined) {
throw new Error("Param Error: other cannot be undefined");
}
const newValues = [];

if (other instanceof Series) {
if (this.dtypes[0] !== other.dtypes[0]) {
Expand All @@ -1310,38 +1307,30 @@ export default class Series extends NDframe {
throw new Error("Param Error must be of same shape");
}

const newValues = [];

this.values.forEach((val, i) => {
newValues.push(Boolean(val) || (other.values[i]));
});

return new Series(newValues, {
config: { ...this.config }
});
} else if (typeof other === "boolean") {
const newValues = [];

this.values.forEach((val) => {
newValues.push(Boolean(val) || (other));
});

return new Series(newValues, {
config: { ...this.config }
});
} else if (Array.isArray(other)) {
const newValues = [];

this.values.forEach((val, i) => {
newValues.push(Boolean(val) || (other[i]));
});

return new Series(newValues, {
config: { ...this.config }
});
} else {
throw new Error("Param Error: other must be a Series, Scalar, or Array of Scalars");
}

return new Series(newValues, {
index: this.index,
config: { ...this.config }
});
}

/**
Expand Down
24 changes: 24 additions & 0 deletions danfojs-browser/tests/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,14 @@ describe("Series", function () {
let expected = [true, false, false, true, true, true, true];
assert.deepEqual(sf.lt(30).values, expected);
});
it("Correct index is returned after operation", function () {
const data1 = [true, true, true, false, false];
const data2 = [true, false, true, true, false];
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });

const expected = ["one", "two", "three", "four", "five"];
assert.deepEqual(sf.lt(data2).index, expected);
});
});

describe("gt", function () {
Expand Down Expand Up @@ -1250,6 +1258,14 @@ describe("Series", function () {
const expected = [true, true, true, true, false];
assert.deepEqual(sf.or(data2).values, expected);
});
it("Correct index is returned after operation", function () {
const data1 = [true, true, true, false, false];
const data2 = [true, false, true, true, false];
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });

const expected = ["one", "two", "three", "four", "five"];
assert.deepEqual(sf.and(data2).index, expected);
});
});

describe("and", function () {
Expand Down Expand Up @@ -1279,6 +1295,14 @@ describe("Series", function () {
const expected = [true, false, true, false, false];
assert.deepEqual(sf.and(data2).values, expected);
});
it("Correct index is returned after operation", function () {
const data1 = [true, true, true, false, false];
const data2 = [true, false, true, true, false];
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });

const expected = ["one", "two", "three", "four", "five"];
assert.deepEqual(sf.and(data2).index, expected);
});

it("Chaining works for logical AND of series and other array (element-wise)", function () {
const data1 = [true, true, true, false, false];
Expand Down
Loading

0 comments on commit a5ba784

Please sign in to comment.