diff --git a/src/traffic/core/flight.py b/src/traffic/core/flight.py index 7513b08f..ea71a588 100644 --- a/src/traffic/core/flight.py +++ b/src/traffic/core/flight.py @@ -1658,6 +1658,7 @@ def resample( self, rule: str | int = "1s", how: None | str | dict[str, Iterable[str]] = "interpolate", + interpolate_kw: dict[str, Any] = {}, projection: None | str | pyproj.Proj | "crs.Projection" = None, ) -> Flight: """Resample the trajectory at a given frequency or for a target number @@ -1680,6 +1681,18 @@ def resample( ``"interpolate"``, ``"ffill"``) and names of columns as values. Columns not included in any value are left as is. + :param interpolate_kw: (default: ``{}``) + + - A dictionary with keyword arguments that will be passed to the + pandas :py:method:`pandas.Series.interpolate` method. + + Example usage: + To specify a fifth-degree polynomial interpolation, you can + pass the following dictionary: + + .. code-block:: python + interpolate_kw = {"method": "polynomial", "order": 5} + :param projection: (default: ``None``) - By default, lat/lon are resampled with a linear interpolation; @@ -1729,7 +1742,8 @@ def resample( for meth, columns in how.items(): if meth is not None: idx = data.columns.get_indexer(columns) - value = getattr(data.iloc[:, idx], meth)() + kwargs = interpolate_kw if meth == "interpolate" else {} + value = getattr(data.iloc[:, idx], meth)(**kwargs) data[data.columns[idx]] = value elif isinstance(rule, int): diff --git a/tests/test_flight.py b/tests/test_flight.py index 8b740089..fe0535d3 100644 --- a/tests/test_flight.py +++ b/tests/test_flight.py @@ -508,6 +508,36 @@ def test_resample_how_argument() -> None: check_dtype=False, ) + altitude_interpolate_quadratic = [ + 30000.0, + 28440.0, + 27160.0, + 26160.0, + 25440.0, + 25000.0, + 24840.0, + 24960.0, + 25360.0, + 26040.0, + 27000.0, + ] + resampled_interpolate_quadratic = Flight(df).resample( + "1s", + how="interpolate", + interpolate_kw={"method": "polynomial", "order": 2}, + ) + pd.testing.assert_frame_equal( + resampled_interpolate_quadratic.data[["altitude", "fake"]], + pd.DataFrame( + { + "altitude": altitude_interpolate_quadratic, + "fake": fake_interpolate, + } + ), + check_dtype=False, + check_exact=False, # accomodate for small rounding errors + ) + resampled_ffill = Flight(df).resample("1s", how="ffill") pd.testing.assert_frame_equal( resampled_ffill.data[["altitude", "fake"]],