Skip to content
This repository was archived by the owner on Sep 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add to_json for dataframe and series, as well as performance improvem…
…ents for .values in constructor
  • Loading branch information
mjclawar committed Feb 1, 2017
commit e4f9483f6166eeea4895414f89b21feca8326531
51 changes: 50 additions & 1 deletion dist/__tests__/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,61 @@ describe('frame', function () {
return "CREATEOBJECTURL";
}
};
// console.log(df.to_excel(new Workbook(), 'my test values', true));
df.to_excel(new _structs.Workbook(), 'my test values', true, { index: false });

window.URL = originalURL;
});
});

describe('to_json', function () {
it('converts a pandas DataFrame to a json object when orient="columns"', function () {
var df = new _frame2.default(_immutable2.default.Map({ x: new _series2.default([1, 2, 3]), y: new _series2.default([2, 3, 4]) }));

var dfJSON = df.to_json();
expect(dfJSON).toEqual({ x: { 0: 1, 1: 2, 2: 3 }, y: { 0: 2, 1: 3, 2: 4 } });
});

it('converts a pandas DataFrame to a json object when orient="records"', function () {
var df = new _frame2.default(_immutable2.default.Map({ x: new _series2.default([1, 2, 3]), y: new _series2.default([2, 3, 4]) }));

var dfJSON = df.to_json({ orient: 'records' });
expect(dfJSON).toBeInstanceOf(Array);
expect(dfJSON[0]).toEqual({ x: 1, y: 2 });
expect(dfJSON[1]).toEqual({ x: 2, y: 3 });
expect(dfJSON[2]).toEqual({ x: 3, y: 4 });
});

it('converts a pandas DataFrame to a json object when orient="split"', function () {
var df = new _frame2.default(_immutable2.default.Map({ x: new _series2.default([1, 2, 3]), y: new _series2.default([2, 3, 4]) }));

var dfJSON = df.to_json({ orient: 'split' });
expect(dfJSON).toBeInstanceOf(Object);
expect(dfJSON.columns).toEqual(['x', 'y']);
expect(dfJSON.index).toEqual([0, 1, 2]);
expect(dfJSON.values).toEqual([[1, 2], [2, 3], [3, 4]]);
});

it('converts a pandas DataFrame to a json object when orient="index"', function () {
var df = new _frame2.default(_immutable2.default.Map({ x: new _series2.default([1, 2, 3]), y: new _series2.default([2, 3, 4]) }));

var dfJSON = df.to_json({ orient: 'index' });
expect(dfJSON).toBeInstanceOf(Object);
expect(dfJSON).toEqual({
0: { x: 1, y: 2 },
1: { x: 2, y: 3 },
2: { x: 3, y: 4 }
});
});

it('converts a pandas DataFrame to a json object when orient="values"', function () {
var df = new _frame2.default(_immutable2.default.Map({ x: new _series2.default([1, 2, 3]), y: new _series2.default([2, 3, 4]) }));

var dfJSON = df.to_json({ orient: 'values' });
expect(dfJSON).toBeInstanceOf(Object);
expect(dfJSON).toEqual([[1, 2], [2, 3], [3, 4]]);
});
});

describe('where', function () {
it('checks for equality of a scalar and returns a DataFrame', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }]);
Expand Down
23 changes: 23 additions & 0 deletions dist/__tests__/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,5 +594,28 @@ describe('series', function () {
}).toThrow();
});
});

describe('to_json', function () {
it('converts a pandas Series to a json object', function () {
var ds = new _series2.default([1, 2, 3, 4], { name: 'x' });

var dsJSON = ds.to_json();
expect(dsJSON).toEqual({ 0: 1, 1: 2, 2: 3, 3: 4 });
});

it('converts a pandas Series to a json object when orient="records"', function () {
var ds = new _series2.default([1, 2, 3, 4], { name: 'x' });

var dsJSON = ds.to_json({ orient: 'records' });
expect(dsJSON).toEqual([1, 2, 3, 4]);
});

it('converts a pandas Series to a json object when orient="split"', function () {
var ds = new _series2.default([1, 2, 3, 4], { name: 'x' });

var dsJSON = ds.to_json({ orient: 'split' });
expect(dsJSON).toEqual({ name: 'x', index: [0, 1, 2, 3], values: [1, 2, 3, 4] });
});
});
});
});
Loading