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 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
Update build, add corr and cov functions to Series and DataFrames
  • Loading branch information
mjclawar committed Jan 11, 2017
commit 9055790d1cad29b0bf6656a17ea48696727ca7ee
24 changes: 24 additions & 0 deletions dist/__tests__/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,30 @@ describe('frame', function () {
});
});

describe('cov', function () {
it('calculates the covariance of all Series in a DataFrame', function () {
var df = new _frame2.default([{ x: 1, y: 2, z: 3 }, { x: 2, y: 1, z: 5 }, { x: 3, y: 0, z: 7 }]);

var dfCov = df.cov();

expect(dfCov.get('x').values.toArray()).toEqual([1, -1, 2]);
expect(dfCov.get('y').values.toArray()).toEqual([-1, 1, -2]);
expect(dfCov.get('z').values.toArray()).toEqual([2, -2, 4]);
});
});

describe('corr', function () {
it('calculates the correlation of all Series in a DataFrame', function () {
var df = new _frame2.default([{ x: 1, y: 2, z: 3 }, { x: 2, y: 1, z: 5 }, { x: 3, y: 0, z: 7 }]);

var dfCorr = df.corr();

expect(dfCorr.get('x').values.toArray()).toEqual([1, -1, 1]);
expect(dfCorr.get('y').values.toArray()).toEqual([-1, 1, -1]);
expect(dfCorr.get('z').values.toArray()).toEqual([1, -1, 1]);
});
});

describe('pct_change', function () {
it('calculates the pct_change along axis 0', function () {
var df1 = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [2, 3, 4] });
Expand Down
44 changes: 44 additions & 0 deletions dist/__tests__/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ describe('series', function () {
expect(ds2.index.toArray()).toEqual([1, 2, 3, 4]);
});

it('shape', function () {
var ds = new _series2.default([1, 2, 3, 4, 5]);
expect(ds.shape).toBeInstanceOf(_immutable2.default.Seq);
expect(ds.shape.toArray()).toEqual([5]);
});

describe('astype', function () {
it('converts a float Series to an integer Series', function () {
var ds1 = new _series2.default([1.5, 2.1, 3.9]);
Expand Down Expand Up @@ -538,5 +544,43 @@ describe('series', function () {
expect(dsFilter.index.toArray()).toEqual([1, 2, 3]);
});
});

describe('cov', function () {
it('calculates the covariance between this Series and another', function () {
var ds1 = new _series2.default([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
var ds2 = new _series2.default([-10, -8, -6, -4, -2, 0, 2, 4, 6, 8]);

var cov = ds1.cov(ds2);
expect(cov).toBeCloseTo(18.3333333, 6);
});

it('throws an error if the Series are not equal length', function () {
var ds1 = new _series2.default([1, 2, 3, 4]);
var ds2 = new _series2.default([2, 3, 4]);

expect(function () {
ds1.cov(ds2);
}).toThrow();
});
});

describe('corr', function () {
it('calculates the correlation between this Series and another', function () {
var ds1 = new _series2.default([1, 2, 3, 4, 5]);
var ds2 = new _series2.default([-10, -8, -6, -4, -2]);

var corr = ds1.corr(ds2);
expect(corr).toBeCloseTo(1, 8);
});

it('throws an error if the Series are not equal length', function () {
var ds1 = new _series2.default([1, 2, 3, 4]);
var ds2 = new _series2.default([2, 3, 4]);

expect(function () {
ds1.corr(ds2);
}).toThrow();
});
});
});
});
158 changes: 117 additions & 41 deletions dist/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);
Expand All @@ -31,6 +43,10 @@ var _immutable2 = _interopRequireDefault(_immutable);

var _exceptions = require('./exceptions');

var _generic = require('./generic');

var _generic2 = _interopRequireDefault(_generic);

var _series = require('./series');

var _series2 = _interopRequireDefault(_series);
Expand Down Expand Up @@ -61,7 +77,9 @@ var parseArrayToSeriesMap = function parseArrayToSeriesMap(array, index) {
return _immutable2.default.Map(dataMap);
};

var DataFrame = function () {
var DataFrame = function (_NDFrame) {
(0, _inherits3.default)(DataFrame, _NDFrame);

/**
* Two-dimensional size-mutable, potentially heterogeneous tabular data
* structure with labeled axes (rows and columns). Arithmetic operations
Expand All @@ -85,29 +103,30 @@ var DataFrame = function () {
* df.toString();
*/
function DataFrame(data) {
var _this = this;

var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _classCallCheck3.default)(this, DataFrame);

var _this = (0, _possibleConstructorReturn3.default)(this, (DataFrame.__proto__ || Object.getPrototypeOf(DataFrame)).call(this, data, kwargs));

if (Array.isArray(data)) {
this._index = (0, _utils.parseIndex)(kwargs.index, _immutable2.default.List(data));
this._data = parseArrayToSeriesMap(data, this._index);
this._columns = this._data.keySeq();
_this.set_axis(0, (0, _utils.parseIndex)(kwargs.index, _immutable2.default.List(data)));
_this._data = parseArrayToSeriesMap(data, _this.index);
_this.set_axis(1, _this._data.keySeq());
} else if (data instanceof _immutable2.default.Map) {
this._data = _immutable2.default.Map(data.keySeq().map(function (k) {
_this._data = _immutable2.default.Map(data.keySeq().map(function (k) {
if (!(data.get(k) instanceof _series2.default)) throw new Error('Map must have [column, series] key-value pairs');

return [k, data.get(k).copy()];
}));
this._columns = this._data.keySeq();
this._index = this._data.get(this.columns.get(0)).index;
_this.set_axis(1, _this._data.keySeq());
_this.set_axis(0, _this._data.get(_this.columns.get(0)).index);
} else if (typeof data === 'undefined') {
this._data = _immutable2.default.Map({});
this._columns = _immutable2.default.Seq.of();
this._index = _immutable2.default.List.of();
_this._data = _immutable2.default.Map({});
_this.set_axis(0, _immutable2.default.List.of());
_this.set_axis(1, _immutable2.default.Seq.of());
}

// TODO this is a slow operation
var valuesList = _immutable2.default.List([]);

var _loop = function _loop(idx) {
Expand All @@ -116,10 +135,12 @@ var DataFrame = function () {
}))]);
};

for (var idx = 0; idx < this.length; idx += 1) {
for (var idx = 0; idx < _this.length; idx += 1) {
_loop(idx);
}
this._values = valuesList;
_this._values = valuesList;
_this._setup_axes(_immutable2.default.List.of(0, 1));
return _this;
}

(0, _createClass3.default)(DataFrame, [{
Expand Down Expand Up @@ -237,7 +258,7 @@ var DataFrame = function () {
}, {
key: 'columnExists',
value: function columnExists(col) {
return this._columns.indexOf(col) >= 0;
return this.columns.indexOf(col) >= 0;
}

/**
Expand Down Expand Up @@ -765,6 +786,81 @@ var DataFrame = function () {

throw new _exceptions.InvalidAxisError();
}
}, {
key: '_pairwiseDataFrame',
value: function _pairwiseDataFrame(func) {
// Apply the func between all Series in the DataFrame, takes two series and returns a value
var valArray = [];

// Calculate upper triangle
for (var idx1 = 0; idx1 < this.columns.size; idx1 += 1) {
valArray.push({});
var ds1 = this.get(this.columns.get(idx1));

for (var idx2 = idx1; idx2 < this.columns.size; idx2 += 1) {
var col2 = this.columns.get(idx2);
var ds2 = this.get(col2);
valArray[idx1][col2] = func(ds1, ds2);
}
}

// Take upper triangle and fill in lower triangle
for (var _idx = 0; _idx < this.columns.size; _idx += 1) {
var col1 = this.columns.get(_idx);
for (var _idx2 = _idx + 1; _idx2 < this.columns.size; _idx2 += 1) {
var _col = this.columns.get(_idx2);
valArray[_idx2][col1] = valArray[_idx][_col];
}
}

return new DataFrame(valArray, { index: this.columns.toList() });
}

/**
* Calculate the covariance between all `Series` in the `DataFrame`
*
* pandas equivalent: [DataFrame.cov](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.cov.html)
*
* @return {DataFrame}
*
* @example
* const df = new DataFrame([{x: 1, y: 2, z: 3}, {x: 2, y: 1, z: 5}, {x: 3, y: 0, z: 7}]);
*
* // Returns DataFrame([{x: 1, y: -1, z: 2}, {x: -1, y: 1, z: -2}, {x: 2, y: -2, z: 4}])
* df.cov();
*/

}, {
key: 'cov',
value: function cov() {
return this._pairwiseDataFrame(function (ds1, ds2) {
return ds1.cov(ds2);
});
}

/**
* Calculate the correlation between all `Series` in the `DataFrame`
*
* pandas equivalent: [DataFrame.corr](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.corr.html)
*
* @return {DataFrame}
*
* @example
* const df = new DataFrame([{x: 1, y: 2, z: 3}, {x: 2, y: 1, z: 5}, {x: 3, y: 0, z: 7}]);
*
* // Returns DataFrame([{x: 1, y: -1, z: 1}, {x: -1, y: 1, z: -1}, {x: 1, y: -1, z: 1}])
* df.corr();
*/

}, {
key: 'corr',
value: function corr() {
// noinspection Eslint
var corrFunc = function corrFunc(ds1, ds2) {
return ds1.values === ds2.values ? 1 : ds1.corr(ds2);
};
return this._pairwiseDataFrame(corrFunc);
}

/**
* Return the percentage change over a given number of periods along the axis
Expand Down Expand Up @@ -859,7 +955,7 @@ var DataFrame = function () {
}, {
key: 'values',
get: function get() {
return this._values;
return (0, _get3.default)(DataFrame.prototype.__proto__ || Object.getPrototypeOf(DataFrame.prototype), 'values', this);
}

/**
Expand All @@ -879,7 +975,7 @@ var DataFrame = function () {
}, {
key: 'columns',
get: function get() {
return this._columns;
return this._get_axis(1);
}

/**
Expand Down Expand Up @@ -913,7 +1009,7 @@ var DataFrame = function () {
});

this._data = _immutable2.default.Map(nextData);
this._columns = _immutable2.default.Seq(columns);
this.set_axis(1, _immutable2.default.Seq(columns));
}

/**
Expand All @@ -931,7 +1027,7 @@ var DataFrame = function () {
}, {
key: 'index',
get: function get() {
return this._index;
return this._get_axis(0);
}

/**
Expand All @@ -953,7 +1049,7 @@ var DataFrame = function () {
set: function set(index) {
var _this11 = this;

this._index = (0, _utils.parseIndex)(index, this._data.get(this._columns.get(0)).values);
this.set_axis(0, (0, _utils.parseIndex)(index, this._data.get(this.columns.get(0)).values));

// noinspection Eslint
this._data.mapEntries(function (_ref9) {
Expand Down Expand Up @@ -989,29 +1085,9 @@ var DataFrame = function () {
return _this12.get(k).length;
}).toArray()));
}

/**
* Return a List representing the dimensionality of the DataFrame
*
* pandas equivalent: [DataFrame.shape](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.shape.html)
*
* @returns {List<number>}
*
* @example
* const df = new DataFrame([{x: 1, y: 2}, {x: 2, y: 3}, {x: 3, y: 4}];
*
* // Returns List [3, 2]
* df.shape;
*/

}, {
key: 'shape',
get: function get() {
return _immutable2.default.List([this.length, this.columns.size]);
}
}]);
return DataFrame;
}();
}(_generic2.default);

exports.default = DataFrame;

Expand Down
Loading