This repository was archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
[Feature] Adding renameSome method for selectively renaming multiple columns at once #91
Open
anusha5695
wants to merge
2
commits into
Gmousse:develop
Choose a base branch
from
anusha5695:renameSome
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| * @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__]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the description by something like:
|
||
| ); | ||
|
|
||
| function customFormat(valueToConvert) { | ||
| return { value: String(Number(valueToConvert) * 10) }; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.