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
rename implemented for frame and series
  • Loading branch information
mjclawar committed May 17, 2017
commit adfe262f80857dc79085ec5e651bbcd3d761b5a5
9 changes: 8 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
[options]
unsafe.enable_getters_and_setters=true
suppress_comment=.*\\$FlowIssue
module.system.node.resolve_dirname=src
module.system.node.resolve_dirname=node_modules

[ignore]
.*\.json
.*/.json
.*/__tests__/.*
.*/node_modules/fbjs/.*

[libs]
node_modules/immutable/dist/immutable.js.flow
14 changes: 14 additions & 0 deletions src/es6/__tests__/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,18 @@ describe('frame', () => {
console.log(df.pivot_table(['a', 'b'], 'c', 'd'));
});
});

describe('rename', () => {
it('renames one Series in the DataFrame', () => {
const df = new DataFrame(
[{x: 1, y: 2}, {x: 2, y: 3}, {x: 3, y: 4}], {index: [1, 2, 3]});
const df2 = df.rename({columns: Immutable.Map({x: 'q'})});

expect(df.columns.toArray()).toEqual(['x', 'y']);
expect(df2.columns.toArray()).toEqual(['q', 'y']);
expect(df2.get('q').values.toArray()).toEqual([1, 2, 3]);
expect(df2.get('q').index.toArray()).toEqual([1, 2, 3]);
expect(df2.get('q').name).toEqual('q');
});
});
});
10 changes: 8 additions & 2 deletions src/es6/__tests__/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ describe('series', () => {
expect(ds2).toBeInstanceOf(Series);
expect(ds2.values.toArray()).toEqual([1, 2, 3, 4]);

ds2.name = 'test';
expect(ds1.name).toEqual('Test name');
expect(ds2.name).toEqual('test');

ds2.index = [1, 2, 3, 4];
expect(ds1.index.toArray()).toEqual([2, 3, 4, 5]);
Expand All @@ -52,6 +50,14 @@ describe('series', () => {
expect(ds.shape.toArray()).toEqual([5]);
});

it('rename', () => {
const ds = new Series([1, 2, 3], {name: 'test name'});
expect(ds.name).toEqual('test name');
const ds2 = ds.rename('test name 2');
expect(ds.name).toEqual('test name');
expect(ds2.name).toEqual('test name 2');
});

describe('astype', () => {
it('converts a float Series to an integer Series', () => {
const ds1 = new Series([1.5, 2.1, 3.9]);
Expand Down
21 changes: 18 additions & 3 deletions src/es6/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* DataFrame object
*/

// $FlowIssue
import Immutable from 'immutable';
// import { saveAs } from 'file-saver'; TODO figure out if best way

Expand Down Expand Up @@ -267,8 +266,7 @@ export default class DataFrame extends NDFrame {
const prevColumn = this.columns.get(idx);
const prevSeries = this.get(prevColumn);

prevSeries.name = k;
nextData[k] = prevSeries;
nextData[k] = prevSeries.rename(k);
});

this._data = Immutable.Map(nextData);
Expand Down Expand Up @@ -1546,6 +1544,23 @@ export default class DataFrame extends NDFrame {
cummin(axis: number = 0): DataFrame {
return this._cumulativeHelper(OP_CUMMIN, axis);
}

/**
* Rename the `DataFrame` and return a new DataFrame
*
* pandas equivalent: [DataFrame.rename](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html)
*
* @param {Immutable.Map} columns
* @returns {DataFrame}
*/
rename({ columns }: {columns: Immutable.Map}): DataFrame {
return new DataFrame(Immutable.OrderedMap(this.columns.map((prevCol) => {
const nextCol = columns.get(prevCol);
if (typeof nextCol === 'undefined')
return [prevCol, this._data.get(prevCol)];
return [nextCol, this._data.get(prevCol).rename(nextCol)];
})), {index: this.index});
}
}

const innerMerge = (df1: DataFrame, df2: DataFrame, on: Array<string|number>): DataFrame => {
Expand Down
39 changes: 32 additions & 7 deletions src/es6/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* numpy.ndarray as the values
*/

// $FlowIssue
import Immutable from 'immutable';

import NDFrame from './generic';
Expand Down Expand Up @@ -59,7 +58,7 @@ export default class Series extends NDFrame {
this._dtype = arrayToDType([data]);
}

this.name = typeof kwargs.name !== 'undefined' ? kwargs.name : '';
this._name = typeof kwargs.name !== 'undefined' ? kwargs.name : '';

this.set_axis(0, parseIndex(kwargs.index, this.values));
this._setup_axes(Immutable.List.of(0));
Expand Down Expand Up @@ -287,6 +286,15 @@ export default class Series extends NDFrame {
return super.values;
}

/**
* Return the name of the `Series`
*
* @returns {string}
*/
get name(): string {
return this._name;
}

/**
* Convert the series to the desired type
*
Expand Down Expand Up @@ -488,7 +496,7 @@ export default class Series extends NDFrame {
return this.map(v => v + val);
else if (val instanceof Series) // $FlowIssue TODO
return this.map((v, idx) => v + val.iloc(idx));
else if (Array.isArray(val)) // $FlowIssue TODO
else if (Array.isArray(val))
return this.map((v, idx) => v + val[idx]);
else if (val instanceof Immutable.List) // $FlowIssue TODO
return this.map((v, idx) => v + val.get(idx));
Expand Down Expand Up @@ -845,11 +853,11 @@ export default class Series extends NDFrame {
this.index.forEach((idx1) => {
if (!(seriesAlignment.has(idx1))) {
seriesAlignment = seriesAlignment
.set(idx1, Immutable.Map({ // $FlowIssue TODO
.set(idx1, Immutable.Map({
first: Immutable.List.of(this.iloc(idx1)),
second: Immutable.List([]),
}));
} else { // $FlowIssue TODO
} else {
seriesAlignment = seriesAlignment.updateIn([idx1, 'first'], l => l.concat(this.iloc(idx1)));
}
});
Expand All @@ -858,11 +866,11 @@ export default class Series extends NDFrame {
if (!(seriesAlignment.has(idx2))) {
seriesAlignment = seriesAlignment
.set(idx2, Immutable.Map({
first: Immutable.List([]), // $FlowIssue TODO
first: Immutable.List([]),
second: Immutable.List.of(series.iloc(idx2)),
}));
} else {
seriesAlignment = seriesAlignment.updateIn([idx2, 'second'], // $FlowIssue TODO
seriesAlignment = seriesAlignment.updateIn([idx2, 'second'],
l => l.concat(series.iloc(idx2)));
}
});
Expand Down Expand Up @@ -1301,4 +1309,21 @@ export default class Series extends NDFrame {
throw new TypeError(`orient must be in ${ALLOWED_ORIENT}`);
}
}

/**
* Rename the `Series` and return a new `Series`
*
* pandas equivalent: [Series.rename](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.rename.html)
*
* @param {string} name
*
* @example
* const ds = new Series([1, 2, 3], {name: 'Test name'});
* ds.rename('New test name');
* // returns 'New test name'
* ds.name;
*/
rename(name: string): Series {
return new Series(this._values, {name, index: this.index});
}
}
13 changes: 13 additions & 0 deletions src/js/__tests__/core/frame.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/js/__tests__/core/series.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 38 additions & 17 deletions src/js/core/frame.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading