Skip to content

Commit a5ba784

Browse files
committed
Update index property after logical operations on Series
1 parent 771f8c9 commit a5ba784

File tree

4 files changed

+191
-165
lines changed

4 files changed

+191
-165
lines changed

danfojs-browser/src/core/series.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,11 @@ export default class Series extends NDframe {
907907
break;
908908
}
909909
}
910-
return new Series(data);
910+
911+
return new Series(data, {
912+
index: this.index,
913+
config: { ...this.config }
914+
});
911915

912916
}
913917

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

12531258
if (other instanceof Series) {
12541259
if (this.dtypes[0] !== other.dtypes[0]) {
@@ -1258,37 +1263,28 @@ export default class Series extends NDframe {
12581263
if (this.shape[0] !== other.shape[0]) {
12591264
throw new Error("Param Error must be of same shape");
12601265
}
1261-
1262-
const newValues = [];
1263-
12641266
this.values.forEach((val, i) => {
12651267
newValues.push(Boolean(val) && Boolean(other.values[i]));
12661268
});
12671269

1268-
return new Series(newValues, {
1269-
config: { ...this.config }
1270-
});
12711270
} else if (Array.isArray(other)) {
1272-
const newValues = [];
12731271

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

1278-
return new Series(newValues, {
1279-
config: { ...this.config }
1280-
});
12811276
} else {
1282-
const newValues = [];
12831277

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

1288-
return new Series(newValues, {
1289-
config: { ...this.config }
1290-
});
12911282
}
1283+
1284+
return new Series(newValues, {
1285+
index: this.index,
1286+
config: { ...this.config }
1287+
});
12921288
}
12931289

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

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

1313-
const newValues = [];
1314-
13151310
this.values.forEach((val, i) => {
13161311
newValues.push(Boolean(val) || (other.values[i]));
13171312
});
13181313

1319-
return new Series(newValues, {
1320-
config: { ...this.config }
1321-
});
13221314
} else if (typeof other === "boolean") {
1323-
const newValues = [];
13241315

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

1329-
return new Series(newValues, {
1330-
config: { ...this.config }
1331-
});
13321320
} else if (Array.isArray(other)) {
1333-
const newValues = [];
13341321

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

1339-
return new Series(newValues, {
1340-
config: { ...this.config }
1341-
});
13421326
} else {
13431327
throw new Error("Param Error: other must be a Series, Scalar, or Array of Scalars");
13441328
}
1329+
1330+
return new Series(newValues, {
1331+
index: this.index,
1332+
config: { ...this.config }
1333+
});
13451334
}
13461335

13471336
/**

danfojs-browser/tests/core/series.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,14 @@ describe("Series", function () {
812812
let expected = [true, false, false, true, true, true, true];
813813
assert.deepEqual(sf.lt(30).values, expected);
814814
});
815+
it("Correct index is returned after operation", function () {
816+
const data1 = [true, true, true, false, false];
817+
const data2 = [true, false, true, true, false];
818+
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
819+
820+
const expected = ["one", "two", "three", "four", "five"];
821+
assert.deepEqual(sf.lt(data2).index, expected);
822+
});
815823
});
816824

817825
describe("gt", function () {
@@ -1250,6 +1258,14 @@ describe("Series", function () {
12501258
const expected = [true, true, true, true, false];
12511259
assert.deepEqual(sf.or(data2).values, expected);
12521260
});
1261+
it("Correct index is returned after operation", function () {
1262+
const data1 = [true, true, true, false, false];
1263+
const data2 = [true, false, true, true, false];
1264+
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
1265+
1266+
const expected = ["one", "two", "three", "four", "five"];
1267+
assert.deepEqual(sf.and(data2).index, expected);
1268+
});
12531269
});
12541270

12551271
describe("and", function () {
@@ -1279,6 +1295,14 @@ describe("Series", function () {
12791295
const expected = [true, false, true, false, false];
12801296
assert.deepEqual(sf.and(data2).values, expected);
12811297
});
1298+
it("Correct index is returned after operation", function () {
1299+
const data1 = [true, true, true, false, false];
1300+
const data2 = [true, false, true, true, false];
1301+
const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
1302+
1303+
const expected = ["one", "two", "three", "four", "five"];
1304+
assert.deepEqual(sf.and(data2).index, expected);
1305+
});
12821306

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

0 commit comments

Comments
 (0)