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
Next Next commit
Update CHANGELOG and README. Update distribution build
  • Loading branch information
mjclawar committed Mar 8, 2017
commit 013ff0138a920e57ad6f0f3f113c153a817f8d70
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

0.1.27
### Added
- `cumsum`, `cummul`, `cummax` and `cummin` for `DataFrame` and `Series`

### Changed
- `pivot` returns `DataFrame` with (now) consistently-sorted columns

0.1.21
### Changed
- Removed uncessary tests from `npm install`.
Expand Down
104 changes: 68 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,97 @@
# pandas-js
Pandas for JavaScript

pandas.js is an open source (experimental) library mimicking the Python [pandas](http://pandas.pydata.org/) library. It relies on [Immutable.js](https://facebook.github.io/immutable-js/) as the [NumPy](http://www.numpy.org/) logical equivalent. The main data objects in pandas.js are the [`pandas.Series`](#series) and the [`pandas.DataFrame`](#dataframe)
pandas.js is an open source (experimental) library mimicking the
Python [pandas](http://pandas.pydata.org/) library. It relies on
[Immutable.js](https://facebook.github.io/immutable-js/) as the
[NumPy](http://www.numpy.org/) base. The main data
objects in pandas.js are the [`Series`](#series) and the
[`DataFrame`](#dataframe)

## Installation and use
```
$ npm install pandas-js
```

Importing [`pandas.Series`](#series) and [`pandas.DataFrame`](#dataframe)
Importing
[`Series`](https://stratodem.github.io/pandas.js-docs/#series)
and
[`DataFrame`](https://stratodem.github.io/pandas.js-docs/#dataframe)
```
import { Series, DataFrame } from 'pandas-js';
```

Create a new `Series`
```
const ds_1 = new Series([1, 2, 3, 4], {name: 'My Data 1'});
console.log('This is a Series');
console.log(ds_1.toString());
console.log(`Sum: ${ds_1.sum()}`);
console.log(`Standard deviation: ${ds_1.std()}`);
const ds = new Series([1, 2, 3, 4], {name: 'My test name', index: [2, 3, 4, 5]})
ds.toString()
// Returns:
// 2 1
// 3 2
// 4 3
// 5 4
// Name: My test name, dtype: dtype(int)
```

const ds_2 = new Series([2, 3, 4, 5], {name: 'My Data 2'});
console.log('Summing two Series:');
console.log(ds_1.add(ds_2).toString());
Filter a `Series`
```
const ds = new Series([1, 2, 3]);

// Returns Series([2, 3]);
ds.filter(ds.gte(2));
```
> This is a Series
0 1
1 2
2 3
Name: My Data 1, dtype: dtype(int)
Sum: 10
Standard deviation: 1
Summing two Series:
0 3
1 5
2 7
3 9
Name: , dtype: dtype(int)

Filtering can be done with generic methods
- [`eq`](https://stratodem.github.io/pandas.js-docs/#series-eq)
- [`lt`](https://stratodem.github.io/pandas.js-docs/#series-lt)
- [`gt`](https://stratodem.github.io/pandas.js-docs/#series-gt)
- [`lte`](https://stratodem.github.io/pandas.js-docs/#series-lte)
- [`gte`](https://stratodem.github.io/pandas.js-docs/#series-gte)

```
const ds = new Series([1, 2, 3], {name: 'Test name'})

// Returns Series([true, false, false])
ds.eq(1);

// Returns Series([false, true, true])
ds.eq(new Series([0, 2, 3]));

// Returns Series([false, true, true])
ds.eq(Immutable.List([0, 2, 3]));

// Returns Series([false, true, true])
ds.eq([0, 2, 3]);

// Returns Series([2, 3])
ds.filter(ds.eq([0, 2, 3]));
```

Create a new `DataFrame`
```
const df = new DataFrame([
{'x': 1, 'y': 2},
{'x': 2, 'y': 3},
{'x': 4, 'y': 5},
]);
const df = new DataFrame([{x: 1, y: 2}, {x: 2, y: 3}, {x: 3, y: 4}])

console.log('This is a DataFrame');
console.log(df.toString());
// Returns:
// x | y
// 0 1 | 2
// 1 2 | 3
// 2 3 | 4
df.toString();
```

Filtering a `DataFrame`

```
This is a DataFrame
| x | y |
--------------
0 | 1 | 2 |
1 | 2 | 3 |
2 | 4 | 5 |
const df = new DataFrame(Immutable.Map({x: new Series([1, 2]), y: new Series([2, 3])}));

// Returns DataFrame(Immutable.Map({x: Series([2]), y: Series([3]));
df.filter(df.get('x').gt(1));

// Returns DataFrame(Immutable.Map({x: Series([2]), y: Series([3]));
df.filter([false, true]);

// Returns DataFrame(Immutable.Map({x: Series([2]), y: Series([3]));
df.filter(Immutable.Map([false, true]));
```

## Documentation
Expand Down
62 changes: 61 additions & 1 deletion dist/__tests__/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,69 @@ describe('frame', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cumsum(1);
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 3, 6]);
expect(df2.get('x').values.toArray()).toEqual([1, 2, 3]);
expect(df2.get('y').values.toArray()).toEqual([3, 5, 7]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});
});

describe('cummul', function () {
it('multiplies along axis 0', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummul();
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 2, 6]);
expect(df2.get('y').values.toArray()).toEqual([2, 6, 24]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});

it('multiplies along axis 1', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummul(1);
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 2, 3]);
expect(df2.get('y').values.toArray()).toEqual([2, 6, 12]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});
});

describe('cummin', function () {
it('Cumulative minimum along axis 0', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummin();
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 1, 1]);
expect(df2.get('y').values.toArray()).toEqual([2, 2, 2]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});

it('Cumulative minimum along axis 1', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummin(1);
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 2, 3]);
expect(df2.get('y').values.toArray()).toEqual([1, 2, 3]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});
});

describe('cummax', function () {
it('Cumulative maximum along axis 0', function () {
var df = new _frame2.default([{ x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummax();
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([1, 2, 3]);
expect(df2.get('y').values.toArray()).toEqual([2, 3, 4]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});

it('Cumulative maximum along axis 1', function () {
var df = new _frame2.default([{ x: 2, y: 1 }, { x: 2, y: 3 }, { x: 7, y: 4 }], { index: [1, 2, 3] });
var df2 = df.cummax(1);
expect(df2).toBeInstanceOf(_frame2.default);
expect(df2.get('x').values.toArray()).toEqual([2, 2, 7]);
expect(df2.get('y').values.toArray()).toEqual([2, 3, 7]);
expect(df2.index.toArray()).toEqual([1, 2, 3]);
});
});
});
38 changes: 32 additions & 6 deletions dist/__tests__/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,38 @@ describe('series', function () {
});
});

describe('cumsum', function () {
var ds = new _series2.default([1, 2, 3, 4, 5], { index: [2, 3, 4, 5, 6] });
var ds2 = ds.cumsum();
expect(ds2).toBeInstanceOf(_series2.default);
expect(ds2.values.toArray()).toEqual([1, 3, 6, 10, 15]);
expect(ds2.index.toArray()).toEqual([2, 3, 4, 5, 6]);
describe('cumulative functions', function () {
it('cumsum', function () {
var ds = new _series2.default([1, 2, 3, 4, 5], { index: [2, 3, 4, 5, 6] });
var ds2 = ds.cumsum();
expect(ds2).toBeInstanceOf(_series2.default);
expect(ds2.values.toArray()).toEqual([1, 3, 6, 10, 15]);
expect(ds2.index.toArray()).toEqual([2, 3, 4, 5, 6]);
});

it('cummul', function () {
var ds = new _series2.default([1, 2, 3, 4, 5], { index: [2, 3, 4, 5, 6] });
var ds2 = ds.cummul();
expect(ds2).toBeInstanceOf(_series2.default);
expect(ds2.values.toArray()).toEqual([1, 2, 6, 24, 120]);
expect(ds2.index.toArray()).toEqual([2, 3, 4, 5, 6]);
});

it('cummax', function () {
var ds = new _series2.default([1, 2, 6, 4, 5], { index: [2, 3, 4, 5, 6] });
var ds2 = ds.cummax();
expect(ds2).toBeInstanceOf(_series2.default);
expect(ds2.values.toArray()).toEqual([1, 2, 6, 6, 6]);
expect(ds2.index.toArray()).toEqual([2, 3, 4, 5, 6]);
});

it('cummin', function () {
var ds = new _series2.default([3, 2, 6, 1, 5], { index: [2, 3, 4, 5, 6] });
var ds2 = ds.cummin();
expect(ds2).toBeInstanceOf(_series2.default);
expect(ds2.values.toArray()).toEqual([3, 2, 2, 1, 1]);
expect(ds2.index.toArray()).toEqual([2, 3, 4, 5, 6]);
});
});
});
});
49 changes: 41 additions & 8 deletions dist/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ var DataFrame = function (_NDFrame) {
var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _classCallCheck3.default)(this, DataFrame);

console.log(typeof data === 'undefined' ? 'undefined' : (0, _typeof3.default)(data));

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

if (Array.isArray(data)) {
Expand All @@ -110,7 +108,18 @@ var DataFrame = function (_NDFrame) {
_this.set_axis(1, _this._data.keySeq());
_this.set_axis(0, _this._data.get(_this.columns.get(0)).index);
} else if (data instanceof _immutable2.default.List) {
throw new TypeError('Invalid type');
var columns = void 0;
if (Array.isArray(kwargs.columns) || kwargs.columns instanceof _immutable2.default.Seq) columns = _immutable2.default.List(kwargs.columns);else if (kwargs.columns instanceof _immutable2.default.List) columns = kwargs.columns;else if (typeof kwargs.columns === 'undefined') columns = _immutable2.default.Range(0, data.get(0).size).toList();else throw new Error('Invalid columns');

_this._values = data;
_this._data = _immutable2.default.OrderedMap(columns.map(function (c, colIdx) {
return [c, new _series2.default(data.map(function (row) {
return row.get(colIdx);
}), { index: kwargs.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.set_axis(0, _immutable2.default.List.of());
Expand Down Expand Up @@ -641,6 +650,7 @@ var DataFrame = function (_NDFrame) {
return new DataFrame(_immutable2.default.Map(this.columns.map(function (k, idx) {
if (idx < periods) return [k, new _series2.default(_immutable2.default.Repeat(null, _this14.length).toList(), { name: k, index: _this14.index })];
var compareCol = _this14.get(_this14.columns.get(idx - periods));

return [k, _this14.get(k).map(function (v, vIdx) {
return v / compareCol.iloc(vIdx) - 1;
})];
Expand Down Expand Up @@ -700,28 +710,51 @@ var DataFrame = function (_NDFrame) {
value: function _cumulativeHelper() {
var _this16 = this;

var operation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'sum';
var operation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _utils.OP_CUMSUM;
var axis = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;

if (axis === 0) {
return new DataFrame(_immutable2.default.Map(this.columns.map(function (c) {
return [c, _this16.get(c).cumulativeHelper(operation)];
return [c, _this16.get(c)._cumulativeHelper(operation)];
})), this.kwargs);
} else if (axis === 1) {
throw new Error('Not supported');
return new DataFrame(this.values.map(function (row) {
return (0, _utils.generateCumulativeFunc)(operation)(row);
}), this.kwargs);
} else throw new Error('invalid axis');
}
}, {
key: 'cumsum',
value: function cumsum() {
var axis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

return this._cumulativeHelper('sum', axis);
return this._cumulativeHelper(_utils.OP_CUMSUM, axis);
}
}, {
key: 'cummul',
value: function cummul() {
var axis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

return this._cumulativeHelper(_utils.OP_CUMMUL, axis);
}
}, {
key: 'cummax',
value: function cummax() {
var axis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

return this._cumulativeHelper(_utils.OP_CUMMAX, axis);
}
}, {
key: 'cummin',
value: function cummin() {
var axis = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

return this._cumulativeHelper(_utils.OP_CUMMIN, axis);
}
}, {
key: 'kwargs',
get: function get() {
return { index: this.index };
return { index: this.index, columns: this.columns };
}
}, {
key: 'values',
Expand Down
Loading