Skip to content

Commit 64a3d63

Browse files
authored
Support "nearest" joinAsof strategy (#287)
This PR aims to support [`nearest` asof join strategy](https://docs.rs/polars/0.43.1/polars/prelude/enum.AsofStrategy.html#variant.Nearest). In brief, it changes: - Add "nearest" type mapping to AsofStrategy::Nearest - Add "nearest" option to typescript's typing - Add testcase Closes #286
1 parent 6e4991e commit 64a3d63

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

__tests__/datelike.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("datelike", () => {
88
.Series([
99
"2016-05-25 13:30:00.023",
1010
"2016-05-25 13:30:00.023",
11-
"2016-05-25 13:30:00.030",
11+
"2016-05-25 13:30:00.035",
1212
"2016-05-25 13:30:00.041",
1313
"2016-05-25 13:30:00.048",
1414
"2016-05-25 13:30:00.049",
@@ -90,7 +90,11 @@ describe("datelike", () => {
9090
out = quotes
9191
.joinAsof(trades, { on: "dates", strategy: "forward", tolerance: "5ms" })
9292
["bid_right"].toArray();
93-
expect(out).toEqual([51.95, 51.95, null, null, 720.77, null, null, null]);
93+
expect(out).toEqual([51.95, 51.95, 51.95, null, 720.77, null, null, null]);
94+
out = quotes
95+
.joinAsof(trades, { on: "dates", strategy: "nearest", tolerance: "5ms" })
96+
["bid_right"].toArray();
97+
expect(out).toEqual([51.95, 51.95, 51.95, 51.95, 98.0, 98.0, null, null]);
9498
});
9599
test("asofjoin tolerance grouper", () => {
96100
const df1 = pl.DataFrame({

polars/dataframe.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,10 @@ export interface DataFrame
771771
* - A "forward" search selects the first row in the right DataFrame whose
772772
* 'on' key is greater than or equal to the left's key.
773773
*
774+
* - A "nearest" search selects the last row in the right DataFrame whose value
775+
* is nearest to the left's key. String keys are not currently supported for a
776+
* nearest search.
777+
*
774778
* The default is "backward".
775779
*
776780
* @param other DataFrame to join with.
@@ -779,7 +783,7 @@ export interface DataFrame
779783
* @param options.on Join column of both DataFrames. If set, `leftOn` and `rightOn` should be undefined.
780784
* @param options.byLeft join on these columns before doing asof join
781785
* @param options.byRight join on these columns before doing asof join
782-
* @param options.strategy One of 'forward', 'backward'
786+
* @param options.strategy One of 'forward', 'backward', 'nearest'
783787
* @param options.suffix Suffix to append to columns with a duplicate name.
784788
* @param options.tolerance
785789
* Numeric tolerance. By setting this the join will only be done if the near keys are within this distance.
@@ -852,7 +856,7 @@ export interface DataFrame
852856
byLeft?: string | string[];
853857
byRight?: string | string[];
854858
by?: string | string[];
855-
strategy?: "backward" | "forward";
859+
strategy?: "backward" | "forward" | "nearest";
856860
suffix?: string;
857861
tolerance?: number | string;
858862
allowParallel?: boolean;

polars/lazy/dataframe.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
253253
- A "forward" search selects the first row in the right DataFrame whose
254254
'on' key is greater than or equal to the left's key.
255255
256+
- A "nearest" search selects the last row in the right DataFrame whose value
257+
is nearest to the left's key. String keys are not currently supported for a
258+
nearest search.
259+
256260
The default is "backward".
257261
258262
Parameters
@@ -263,7 +267,7 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
263267
@param options.on Join column of both DataFrames. If set, `leftOn` and `rightOn` should be undefined.
264268
@param options.byLeft join on these columns before doing asof join
265269
@param options.byRight join on these columns before doing asof join
266-
@param options.strategy One of {'forward', 'backward'}
270+
@param options.strategy One of {'forward', 'backward', 'nearest'}
267271
@param options.suffix Suffix to append to columns with a duplicate name.
268272
@param options.tolerance
269273
Numeric tolerance. By setting this the join will only be done if the near keys are within this distance.
@@ -337,7 +341,7 @@ export interface LazyDataFrame extends Serialize, GroupByOps<LazyGroupBy> {
337341
byLeft?: string | string[];
338342
byRight?: string | string[];
339343
by?: string | string[];
340-
strategy?: "backward" | "forward";
344+
strategy?: "backward" | "forward" | "nearest";
341345
suffix?: string;
342346
tolerance?: number | string;
343347
allowParallel?: boolean;

src/lazy/dataframe.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ impl JsLazyFrame {
307307
let strategy = match strategy.as_ref() {
308308
"forward" => AsofStrategy::Forward,
309309
"backward" => AsofStrategy::Backward,
310-
_ => panic!("expected one of {{'forward', 'backward'}}"),
310+
"nearest" => AsofStrategy::Nearest,
311+
_ => panic!("expected one of {{'forward', 'backward', 'nearest'}}"),
311312
};
312313
let ldf = self.ldf.clone();
313314
let other = other.ldf.clone();

0 commit comments

Comments
 (0)