Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
Open
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
10 changes: 10 additions & 0 deletions dataframe-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class DataFrame {

renameAll(newColumnNames: any): any;

renameSome(columnsMap: Map<any,any>): any;

replace(value: any, replacement: any, columnNames: any): any;

restructure(newColumnNames: any): any;
Expand Down Expand Up @@ -274,6 +276,8 @@ export namespace DataFrame {

function renameAll(newColumnNames: any): any;

function renameSome(columnsMap: Map<any,any>): any;

function replace(value: any, replacement: any, columnNames: any): any;

function restructure(newColumnNames: any): any;
Expand Down Expand Up @@ -504,6 +508,12 @@ export namespace DataFrame {

}

namespace renameSome {
const prototype: {
};

}

namespace replace {
const prototype: {
};
Expand Down
11 changes: 11 additions & 0 deletions lib/dataframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,17 @@ var DataFrame = function () {

return this.renameAll(newColumnNames);
}
}, {
key: "renameSome",
value: function renameSome(columnsMap) {
let availableColumnNames = this[__columns__];
Object.entries(columnsMap).forEach(([key,value])=>{
let position = availableColumnNames.indexOf(key);
position > -1 && (availableColumnNames[position] = value);
});

return this.renameAll(availableColumnNames);
}
}, {
key: "castAll",
value: function castAll(typeFunctions) {
Expand Down
17 changes: 17 additions & 0 deletions src/dataframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,23 @@ class DataFrame {
return this.renameAll(newColumnNames);
}

/**
* Rename selective columns.
* @param {Map} columnsMap Key value pairs of columns to rename and new column names of the DataFrame.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider replacing Map (in the description) by an Object.

Indeed, it could confuse the users with the real Map type https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map.

* @returns {DataFrame} A new DataFrame with the new column names.
* @example
* df.renameSome({'column1':'columnA', 'column3':'columnB', 'column50':'columnC'])
*/
renameSome(columnsMap) {
let availableColumnNames = this[__columns__];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid mutations in the current DataFrame by setting the array index, consider to use:

const availableColumnNames = Array.from(this[__columns__]);

Object.entries(columnsMap).forEach(([key,value])=>{
let position = availableColumnNames.indexOf(key);
position > -1 && (availableColumnNames[position] = value);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (position > -1) {
    availableColumnNames[position] = value
}

Your syntax is good, but it could confuse newcomers. Prefer to use a simple if statement in this case.

});

return this.renameAll(availableColumnNames);
}

/**
* Cast each column into a given type.
* @param {Array} typeFunctions The functions used to cast columns.
Expand Down
29 changes: 29 additions & 0 deletions tests/dataframe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ test("DataFrame columns can be", assert => {
"renamed individually."
);

assert.deepEqual(
df
.select("c2", "c3", "c4")
.renameSome({"c2":"c16", "c3":"c17", "c4":"c18"})
.listColumns(),
["c16", "c17", "c18"],
"renamed selectively."
);



assert.deepEqual(
df
.select("c2", "c3", "c4")
.renameSome({"c2":"c16", "c3":"c17", "c4":"c18"})
.toArray()[0],
[6, 9, 10],
"renamed selectively without altering data."
);

assert.deepEqual(
df
.select("c2", "c3", "c4")
.renameSome({})
.listColumns(),
["c2", "c3", "c4"],
"renamed selectively"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace the description by something like:

renamed nothing if the payload is empty.

);

function customFormat(valueToConvert) {
return { value: String(Number(valueToConvert) * 10) };
}
Expand Down