Skip to content
Open
Show file tree
Hide file tree
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
Expand Up @@ -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 });
Expand Down
14 changes: 14 additions & 0 deletions src/danfojs-node/test/core/frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down