Skip to content

Map and apply function to perform custom operations #46

@tcpessoa

Description

@tcpessoa

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! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions