Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map and apply function to perform custom operations #46

Open
tcpessoa opened this issue Mar 1, 2023 · 3 comments
Open

Map and apply function to perform custom operations #46

tcpessoa opened this issue Mar 1, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@tcpessoa
Copy link

tcpessoa commented Mar 1, 2023

Ability to perform custom operations by using the map and apply functions

Feature demonstration with expected result

I would like to have the ability to perform a custom operation on a Series/Expr as such:

import pl from 'nodejs-polars';

const data = [
	{ a: 'A', b: 10},
	{ a: 'B', b: 20},
	{ a: 'B', b: 13},
	{ a: 'C', b: 40},
];

const mapping = { D: 'OtherD'};

const df = pl.DataFrame(data);
const dfWithOtherA = df.withColumns([pl.col("a").map((val) => mapping[val]).alias('otherA')]);

/* expected result:
  ┌─────┬──────┬────────┐
  │ a   ┆ b    ┆ otherA │
  │ --- ┆ ---  ┆ ---    │
  │ str ┆ f64  ┆ str    │
  ╞═════╪══════╪════════╡
  │ A   ┆ 10.0 ┆ A      │
  ├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
  │ B   ┆ 20.0 ┆ OtherB │
  ├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
  │ B   ┆ 13.0 ┆ OtherB │
  ├╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
  │ C   ┆ 40.0 ┆ C      │
  └─────┴──────┴────────┘
*/

The expected behavior should follow what is described in the polars docs.

Other comments

I would be happy to help with some guidance, but I still am not aware of polars internals to go for a PR.
Thank you for the awesome work in all other APIs and functions, keep the great work! :)

@ChakshuGautam
Copy link

I would love to help as well!!

@universalmind303
Copy link
Collaborator

so this is actually one of the more challenging ones to implement. It requires extensive knowledge of the n-api framework, as well as the rust polars internals. If anyone is up for a challenge, you can likely take a look at the python implementation to get a general idea of what they are doing.

Keep in mind, Pyo3 is a bit more flexible in what you can do than n-api is, so it will be a bit more challenging than the python implementation.

@ritchie46
Copy link
Member

You could go anyvalue only. That's a bit slower, but much easier to implement.

@universalmind303 universalmind303 added the enhancement New feature or request label Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants