Skip to content

Commit

Permalink
Fix column name not displayed after aggregative functions are applied
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Oct 2, 2021
1 parent 26de165 commit 39e9757
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 36 deletions.
80 changes: 56 additions & 24 deletions danfojs-browser/src/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,11 @@ export default class DataFrame extends NDframe {

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => arr.reduce((a, b) => a + b, 0) / arr.length);
return new Series(meanArr);
if (axis === 1) {
return new Series(meanArr, { index: this.columns });
} else {
return new Series(meanArr, { index: this.index });
}
}

/**
Expand All @@ -708,8 +712,12 @@ export default class DataFrame extends NDframe {
}

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => median(arr));
return new Series(meanArr);
const medianArr = newData.map((arr) => median(arr));
if (axis === 1) {
return new Series(medianArr, { index: this.columns });
} else {
return new Series(medianArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => {
const modeArr = newData.map((arr) => {
const tempMode = mode(arr);
if (tempMode.length === 1) {
return tempMode[0];
} else {
return tempMode[keep];
}
});
return new Series(meanArr);

if (axis === 1) {
return new Series(modeArr, { index: this.columns });
} else {
return new Series(modeArr, { index: this.index });
}
}

/**
Expand All @@ -766,7 +777,12 @@ export default class DataFrame extends NDframe {
}
return smallestValue;
});
return new Series(minArr);

if (axis === 1) {
return new Series(minArr, { index: this.columns });
} else {
return new Series(minArr, { index: this.index });
}

}

Expand Down Expand Up @@ -794,7 +810,12 @@ export default class DataFrame extends NDframe {
}
return biggestValue;
});
return new Series(maxArr);

if (axis === 1) {
return new Series(maxArr, { index: this.columns });
} else {
return new Series(maxArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const stdArr = newData.map((arr) => std(arr));
return new Series(stdArr);

if (axis === 1) {
return new Series(stdArr, { index: this.columns });
} else {
return new Series(stdArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const varArr = newData.map((arr) => variance(arr));
return new Series(varArr);

if (axis === 1) {
return new Series(varArr, { index: this.columns });
} else {
return new Series(varArr, { index: this.index });
}
}

/**
Expand Down Expand Up @@ -973,8 +1002,11 @@ export default class DataFrame extends NDframe {

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const countArr = newData.map((arr) => arr.length);
return new Series(countArr);

if (axis === 1) {
return new Series(countArr, { index: this.columns });
} else {
return new Series(countArr, { index: this.index });
}
}

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

if (condition) {
Expand Down Expand Up @@ -2281,19 +2313,19 @@ export default class DataFrame extends NDframe {
* df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-', inplace: true })
* df.getDummies({ columns: ['a', 'b'], prefix: ['col1', 'col2'], prefixSeparator: '-', inplace: true })
*/
get_dummies(options) {
const { inplace } = { inplace: false, ...options };

const encodedDF = dummyEncode(this, options);
if (inplace) {
this.$setValues(encodedDF.values, false, false);
this.$setColumnNames(encodedDF.columns);
} else {
return encodedDF;
}
get_dummies(options) {
const { inplace } = { inplace: false, ...options };

const encodedDF = dummyEncode(this, options);
if (inplace) {
this.$setValues(encodedDF.values, false, false);
this.$setColumnNames(encodedDF.columns);
} else {
return encodedDF;
}

}

/**
* Make plots of Series or DataFrame.
* Uses the Plotly as backend, so supports Plotly's configuration parameters
Expand Down
2 changes: 2 additions & 0 deletions danfojs-browser/tests/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ describe("DataFrame", function () {
let data = [[0, 2, 4], [360, 180, 360]];
let df = new dfd.DataFrame(data, { columns: ["col1", "col2", "col3"] });
assert.deepEqual(df.mean().values, [180, 91, 182]);
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"]);

});
it("Return mean of a DataFrame along axis 0 (row)", function () {
let data = [[0, 2, 4], [360, 180, 360]];
Expand Down
58 changes: 46 additions & 12 deletions danfojs-node/src/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,11 @@ export default class DataFrame extends NDframe {

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => arr.reduce((a, b) => a + b, 0) / arr.length);
return new Series(meanArr);
if (axis === 1) {
return new Series(meanArr, { index: this.columns });
} else {
return new Series(meanArr, { index: this.index });
}
}

/**
Expand All @@ -708,8 +712,12 @@ export default class DataFrame extends NDframe {
}

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => median(arr));
return new Series(meanArr);
const medianArr = newData.map((arr) => median(arr));
if (axis === 1) {
return new Series(medianArr, { index: this.columns });
} else {
return new Series(medianArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const meanArr = newData.map((arr) => {
const modeArr = newData.map((arr) => {
const tempMode = mode(arr);
if (tempMode.length === 1) {
return tempMode[0];
} else {
return tempMode[keep];
}
});
return new Series(meanArr);

if (axis === 1) {
return new Series(modeArr, { index: this.columns });
} else {
return new Series(modeArr, { index: this.index });
}
}

/**
Expand All @@ -766,7 +777,12 @@ export default class DataFrame extends NDframe {
}
return smallestValue;
});
return new Series(minArr);

if (axis === 1) {
return new Series(minArr, { index: this.columns });
} else {
return new Series(minArr, { index: this.index });
}

}

Expand Down Expand Up @@ -794,7 +810,12 @@ export default class DataFrame extends NDframe {
}
return biggestValue;
});
return new Series(maxArr);

if (axis === 1) {
return new Series(maxArr, { index: this.columns });
} else {
return new Series(maxArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const stdArr = newData.map((arr) => std(arr));
return new Series(stdArr);

if (axis === 1) {
return new Series(stdArr, { index: this.columns });
} else {
return new Series(stdArr, { index: this.index });
}

}

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

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const varArr = newData.map((arr) => variance(arr));
return new Series(varArr);

if (axis === 1) {
return new Series(varArr, { index: this.columns });
} else {
return new Series(varArr, { index: this.index });
}
}

/**
Expand Down Expand Up @@ -973,7 +1002,12 @@ export default class DataFrame extends NDframe {

const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
const countArr = newData.map((arr) => arr.length);
return new Series(countArr);

if (axis === 1) {
return new Series(countArr, { index: this.columns });
} else {
return new Series(countArr, { index: this.index });
}

}

Expand Down
2 changes: 2 additions & 0 deletions danfojs-node/tests/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ describe("DataFrame", function () {
let data = [[0, 2, 4], [360, 180, 360]];
let df = new DataFrame(data, { columns: ["col1", "col2", "col3"] });
assert.deepEqual(df.mean().values, [180, 91, 182]);
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"] );

});
it("Return mean of a DataFrame along axis 0 (row)", function () {
let data = [[0, 2, 4], [360, 180, 360]];
Expand Down

0 comments on commit 39e9757

Please sign in to comment.