Skip to content

Commit

Permalink
fix #466 flight.split
Browse files Browse the repository at this point in the history
  • Loading branch information
xoolive committed Oct 28, 2024
1 parent a42fca2 commit 78baf38
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/traffic/core/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,17 @@ def _split(
# This method helps splitting a flight into several.
if data.shape[0] < 2:
return
diff = data.timestamp.diff().values
diff = data.timestamp.diff()
if unit is None:
delta = pd.Timedelta(value).to_timedelta64()
else:
delta = np.timedelta64(value, unit)
# There seems to be a change with numpy >= 1.18
# max() now may return NaN, therefore the following fix
max_ = np.nanmax(diff)
max_ = diff.max()
if max_ > delta:
# np.nanargmax seems bugged with timestamps
argmax = np.where(diff == max_)[0][0]
argmax = diff.argmax()
yield from _split(data.iloc[:argmax], value, unit)
yield from _split(data.iloc[argmax:], value, unit)
else:
Expand Down Expand Up @@ -1490,7 +1490,7 @@ def split(
- in the NumPy style: ``Flight.split(10, 'm')`` (see
``np.timedelta64``);
- in the pandas style: ``Flight.split('10T')`` (see ``pd.Timedelta``)
- in the pandas style: ``Flight.split('10 min')`` (see ``pd.Timedelta``)
If the `condition` parameter is set, the flight is split between two
segments only if `condition(f1, f2)` is verified.
Expand Down Expand Up @@ -2720,6 +2720,7 @@ def query_ehs(
self,
data: Union[None, pd.DataFrame, "RawData"] = None,
failure_mode: str = "info",
**kwargs: Any,
) -> Flight:
"""Extends data with extra columns from EHS messages.
Expand Down Expand Up @@ -2779,7 +2780,9 @@ def fail_silent() -> Flight:
failure = failure_dict[failure_mode]

if data is None:
ext = opensky.extended(self.start, self.stop, icao24=self.icao24)
ext = opensky.extended(
self.start, self.stop, icao24=self.icao24, **kwargs
)
df = ext.data if ext is not None else None
else:
df = data if isinstance(data, pd.DataFrame) else data.data
Expand All @@ -2793,19 +2796,17 @@ def fail_silent() -> Flight:

timestamped_df = (
df.sort_values("mintime")
.assign(
timestamp=lambda df: pd.to_datetime(
df.mintime, unit="s", utc=True
)
)
.assign(timestamp=lambda df: df.mintime)
# TODO shouldn't be necessary after pyopensky 2.10
.convert_dtypes(dtype_backend="pyarrow")
)

referenced_df = (
timestamped_df.merge(
# TODO shouldn't be necessary after pyopensky 2.10
self.data.convert_dtypes(dtype_backend="pyarrow"),
self.data.convert_dtypes(dtype_backend="pyarrow").assign(
timestamp=lambda df: df.timestamp.astype("int64") * 1e-9
),
on="timestamp",
how="outer",
)
Expand All @@ -2829,7 +2830,7 @@ def fail_silent() -> Flight:

decoded = rs1090.decode(
referenced_df.rawmsg,
referenced_df.timestamp.astype("int64") * 1e-9,
referenced_df.timestamp.astype("int64"),
)

if len(decoded) == 0:
Expand Down

0 comments on commit 78baf38

Please sign in to comment.