Skip to content

Commit 39e9757

Browse files
committed
Fix column name not displayed after aggregative functions are applied
1 parent 26de165 commit 39e9757

File tree

4 files changed

+106
-36
lines changed

4 files changed

+106
-36
lines changed

danfojs-browser/src/core/frame.js

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,11 @@ export default class DataFrame extends NDframe {
689689

690690
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
691691
const meanArr = newData.map((arr) => arr.reduce((a, b) => a + b, 0) / arr.length);
692-
return new Series(meanArr);
692+
if (axis === 1) {
693+
return new Series(meanArr, { index: this.columns });
694+
} else {
695+
return new Series(meanArr, { index: this.index });
696+
}
693697
}
694698

695699
/**
@@ -708,8 +712,12 @@ export default class DataFrame extends NDframe {
708712
}
709713

710714
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
711-
const meanArr = newData.map((arr) => median(arr));
712-
return new Series(meanArr);
715+
const medianArr = newData.map((arr) => median(arr));
716+
if (axis === 1) {
717+
return new Series(medianArr, { index: this.columns });
718+
} else {
719+
return new Series(medianArr, { index: this.index });
720+
}
713721

714722
}
715723

@@ -730,16 +738,19 @@ export default class DataFrame extends NDframe {
730738
}
731739

732740
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
733-
const meanArr = newData.map((arr) => {
741+
const modeArr = newData.map((arr) => {
734742
const tempMode = mode(arr);
735743
if (tempMode.length === 1) {
736744
return tempMode[0];
737745
} else {
738746
return tempMode[keep];
739747
}
740748
});
741-
return new Series(meanArr);
742-
749+
if (axis === 1) {
750+
return new Series(modeArr, { index: this.columns });
751+
} else {
752+
return new Series(modeArr, { index: this.index });
753+
}
743754
}
744755

745756
/**
@@ -766,7 +777,12 @@ export default class DataFrame extends NDframe {
766777
}
767778
return smallestValue;
768779
});
769-
return new Series(minArr);
780+
781+
if (axis === 1) {
782+
return new Series(minArr, { index: this.columns });
783+
} else {
784+
return new Series(minArr, { index: this.index });
785+
}
770786

771787
}
772788

@@ -794,7 +810,12 @@ export default class DataFrame extends NDframe {
794810
}
795811
return biggestValue;
796812
});
797-
return new Series(maxArr);
813+
814+
if (axis === 1) {
815+
return new Series(maxArr, { index: this.columns });
816+
} else {
817+
return new Series(maxArr, { index: this.index });
818+
}
798819

799820
}
800821

@@ -815,7 +836,12 @@ export default class DataFrame extends NDframe {
815836

816837
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
817838
const stdArr = newData.map((arr) => std(arr));
818-
return new Series(stdArr);
839+
840+
if (axis === 1) {
841+
return new Series(stdArr, { index: this.columns });
842+
} else {
843+
return new Series(stdArr, { index: this.index });
844+
}
819845

820846
}
821847

@@ -836,8 +862,11 @@ export default class DataFrame extends NDframe {
836862

837863
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
838864
const varArr = newData.map((arr) => variance(arr));
839-
return new Series(varArr);
840-
865+
if (axis === 1) {
866+
return new Series(varArr, { index: this.columns });
867+
} else {
868+
return new Series(varArr, { index: this.index });
869+
}
841870
}
842871

843872
/**
@@ -973,8 +1002,11 @@ export default class DataFrame extends NDframe {
9731002

9741003
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
9751004
const countArr = newData.map((arr) => arr.length);
976-
return new Series(countArr);
977-
1005+
if (axis === 1) {
1006+
return new Series(countArr, { index: this.columns });
1007+
} else {
1008+
return new Series(countArr, { index: this.index });
1009+
}
9781010
}
9791011

9801012
/**
@@ -2182,7 +2214,7 @@ export default class DataFrame extends NDframe {
21822214
* If specified, then other parameters are ignored.
21832215
* - `inplace` Boolean indicating whether to perform the operation inplace or not. Defaults to false
21842216
**/
2185-
query(options) {
2217+
query(options) {
21862218
const { inplace, condition, column, is, to } = { inplace: false, ...options };
21872219

21882220
if (condition) {
@@ -2281,19 +2313,19 @@ export default class DataFrame extends NDframe {
22812313
* df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-', inplace: true })
22822314
* df.getDummies({ columns: ['a', 'b'], prefix: ['col1', 'col2'], prefixSeparator: '-', inplace: true })
22832315
*/
2284-
get_dummies(options) {
2285-
const { inplace } = { inplace: false, ...options };
2286-
2287-
const encodedDF = dummyEncode(this, options);
2288-
if (inplace) {
2289-
this.$setValues(encodedDF.values, false, false);
2290-
this.$setColumnNames(encodedDF.columns);
2291-
} else {
2292-
return encodedDF;
2293-
}
2316+
get_dummies(options) {
2317+
const { inplace } = { inplace: false, ...options };
22942318

2319+
const encodedDF = dummyEncode(this, options);
2320+
if (inplace) {
2321+
this.$setValues(encodedDF.values, false, false);
2322+
this.$setColumnNames(encodedDF.columns);
2323+
} else {
2324+
return encodedDF;
22952325
}
22962326

2327+
}
2328+
22972329
/**
22982330
* Make plots of Series or DataFrame.
22992331
* Uses the Plotly as backend, so supports Plotly's configuration parameters

danfojs-browser/tests/core/frame.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ describe("DataFrame", function () {
628628
let data = [[0, 2, 4], [360, 180, 360]];
629629
let df = new dfd.DataFrame(data, { columns: ["col1", "col2", "col3"] });
630630
assert.deepEqual(df.mean().values, [180, 91, 182]);
631+
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"]);
632+
631633
});
632634
it("Return mean of a DataFrame along axis 0 (row)", function () {
633635
let data = [[0, 2, 4], [360, 180, 360]];

danfojs-node/src/core/frame.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,11 @@ export default class DataFrame extends NDframe {
689689

690690
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
691691
const meanArr = newData.map((arr) => arr.reduce((a, b) => a + b, 0) / arr.length);
692-
return new Series(meanArr);
692+
if (axis === 1) {
693+
return new Series(meanArr, { index: this.columns });
694+
} else {
695+
return new Series(meanArr, { index: this.index });
696+
}
693697
}
694698

695699
/**
@@ -708,8 +712,12 @@ export default class DataFrame extends NDframe {
708712
}
709713

710714
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
711-
const meanArr = newData.map((arr) => median(arr));
712-
return new Series(meanArr);
715+
const medianArr = newData.map((arr) => median(arr));
716+
if (axis === 1) {
717+
return new Series(medianArr, { index: this.columns });
718+
} else {
719+
return new Series(medianArr, { index: this.index });
720+
}
713721

714722
}
715723

@@ -730,16 +738,19 @@ export default class DataFrame extends NDframe {
730738
}
731739

732740
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
733-
const meanArr = newData.map((arr) => {
741+
const modeArr = newData.map((arr) => {
734742
const tempMode = mode(arr);
735743
if (tempMode.length === 1) {
736744
return tempMode[0];
737745
} else {
738746
return tempMode[keep];
739747
}
740748
});
741-
return new Series(meanArr);
742-
749+
if (axis === 1) {
750+
return new Series(modeArr, { index: this.columns });
751+
} else {
752+
return new Series(modeArr, { index: this.index });
753+
}
743754
}
744755

745756
/**
@@ -766,7 +777,12 @@ export default class DataFrame extends NDframe {
766777
}
767778
return smallestValue;
768779
});
769-
return new Series(minArr);
780+
781+
if (axis === 1) {
782+
return new Series(minArr, { index: this.columns });
783+
} else {
784+
return new Series(minArr, { index: this.index });
785+
}
770786

771787
}
772788

@@ -794,7 +810,12 @@ export default class DataFrame extends NDframe {
794810
}
795811
return biggestValue;
796812
});
797-
return new Series(maxArr);
813+
814+
if (axis === 1) {
815+
return new Series(maxArr, { index: this.columns });
816+
} else {
817+
return new Series(maxArr, { index: this.index });
818+
}
798819

799820
}
800821

@@ -815,7 +836,12 @@ export default class DataFrame extends NDframe {
815836

816837
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
817838
const stdArr = newData.map((arr) => std(arr));
818-
return new Series(stdArr);
839+
840+
if (axis === 1) {
841+
return new Series(stdArr, { index: this.columns });
842+
} else {
843+
return new Series(stdArr, { index: this.index });
844+
}
819845

820846
}
821847

@@ -836,8 +862,11 @@ export default class DataFrame extends NDframe {
836862

837863
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
838864
const varArr = newData.map((arr) => variance(arr));
839-
return new Series(varArr);
840-
865+
if (axis === 1) {
866+
return new Series(varArr, { index: this.columns });
867+
} else {
868+
return new Series(varArr, { index: this.index });
869+
}
841870
}
842871

843872
/**
@@ -973,7 +1002,12 @@ export default class DataFrame extends NDframe {
9731002

9741003
const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
9751004
const countArr = newData.map((arr) => arr.length);
976-
return new Series(countArr);
1005+
1006+
if (axis === 1) {
1007+
return new Series(countArr, { index: this.columns });
1008+
} else {
1009+
return new Series(countArr, { index: this.index });
1010+
}
9771011

9781012
}
9791013

danfojs-node/tests/core/frame.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ describe("DataFrame", function () {
644644
let data = [[0, 2, 4], [360, 180, 360]];
645645
let df = new DataFrame(data, { columns: ["col1", "col2", "col3"] });
646646
assert.deepEqual(df.mean().values, [180, 91, 182]);
647+
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"] );
648+
647649
});
648650
it("Return mean of a DataFrame along axis 0 (row)", function () {
649651
let data = [[0, 2, 4], [360, 180, 360]];

0 commit comments

Comments
 (0)