Skip to content

fix(dataframe): ensure consistent column order during creation from objects #668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/danfojs-base/core/generic.ts
Original file line number Diff line number Diff line change
@@ -133,16 +133,16 @@ export default class NDframe implements NDframeInterface {
*/
private loadObjectIntoNdframe({ data, type, index, columns, dtypes }: LoadObjectDataType): void {
if (type === 1 && Array.isArray(data)) {
const _data = (data).map((item) => {
return Object.values(item);
});
let _columnNames = Object.keys(data[0]);

let _columnNames;
const _data = data.map((item: Record<string, any>) => {
return _columnNames.map((col: string) => {
return item[col];
})
});

if (columns) {
_columnNames = columns
} else {
_columnNames = Object.keys((data)[0]);
}

this.loadArrayIntoNdframe({ data: _data, index, columns: _columnNames, dtypes });
14 changes: 14 additions & 0 deletions src/danfojs-node/test/core/frame.test.ts
Original file line number Diff line number Diff line change
@@ -83,6 +83,20 @@ describe("DataFrame", function () {
assert.deepEqual(df.values[3], [1, 4, 90.1]);
assert.deepEqual(df.dtypes, ["float32", "int32", "float32",]);
});

it("correctly converts to column-oriented JSON", function () {
const jsonData = [
{ campaignId: "toyota", agentId: "bob", metricValue: 1, metricId: "callCount" },
{ campaignId: "toyota", agentId: "jim", metricValue: 2, metricId: "callCount" },
{ campaignId: "sony", agentId: "ben", metricValue: 3, metricId: "callCount" },
{ campaignId: "sony", agentId: "karl", metricId: "callCount", metricValue: 4 },
];

const df = new DataFrame(jsonData);
const columnJSON = df.toJSON({ format: "column" });

assert.deepEqual(columnJSON, jsonData);
});
})

describe("addColumn", function () {