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

Support "nearest" joinAsof strategy #286

Closed
StephLin opened this issue Oct 12, 2024 · 0 comments · Fixed by #287
Closed

Support "nearest" joinAsof strategy #286

StephLin opened this issue Oct 12, 2024 · 0 comments · Fixed by #287

Comments

@StephLin
Copy link
Contributor

StephLin commented Oct 12, 2024

Describe your feature request

Rust and Python versions support nearest asof join strategy, while nodejs version does not yet. It would be great if nodejs version do as well. I'm working on this feature, and following are possible changes and test cases.

Possible changes

  1. src/lazy/dataframe.rs

     let strategy = match strategy.as_ref() { 
         "forward" => AsofStrategy::Forward, 
         "backward" => AsofStrategy::Backward,
    +    "nearest" => AsofStrategy::Nearest,
         _ => panic!("expected one of {{'forward', 'backward'}}"), 
     }; 
  2. polars/lazy/dataframe.ts

    -     strategy?: "backward" | "forward";
    +     strategy?: "backward" | "forward" | "nearest";
  3. polars/dataframe.ts

    -     strategy?: "backward" | "forward";
    +     strategy?: "backward" | "forward" | "nearest";

Test cases

let gdp = pl.DataFrame({
   date: [
     new Date('2016-01-01'),
     new Date('2017-01-01'),
     new Date('2018-01-01'),
     new Date('2019-01-01'),
   ],  // note record date: Jan 1st (sorted!)
   gdp: [4164, 4411, 4566, 4696],
})

let population = pl.DataFrame({
   date: [
     new Date('2016-05-12'),
     new Date('2017-05-12'),
     new Date('2017-10-12'),
     new Date('2018-05-12'),
     new Date('2018-10-12'),
     new Date('2019-05-12'),
   ],  // note record date: May 12th (sorted!)
   "population": [82.19, 82.66, 82.95, 83.12, 83.37, 83.52],
})

population.joinAsof(
   gdp,
   {leftOn:"date", rightOn:"date", strategy:"nearest"}
)

It should output

  shape: (6, 3)
  ┌─────────────────────┬────────────┬────────┐
  │ date                ┆ population ┆ gdp    │
  │ ---                 ┆ ---        ┆ ---    │
  │ datetime[ms]        ┆ f64        ┆ f64    │
  ╞═════════════════════╪════════════╪════════╡
  │ 2016-05-12 00:00:00 ┆ 82.19      ┆ 4164.0 │
  │ 2017-05-12 00:00:00 ┆ 82.66      ┆ 4411.0 │
  │ 2017-10-12 00:00:00 ┆ 82.95      ┆ 4566.0 │
  │ 2018-05-12 00:00:00 ┆ 83.12      ┆ 4566.0 │
  │ 2018-10-12 00:00:00 ┆ 83.37      ┆ 4696.0 │
  │ 2019-05-12 00:00:00 ┆ 83.52      ┆ 4696.0 │
  └─────────────────────┴────────────┴────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant