@@ -1456,14 +1456,27 @@ def sliding_windows(
14561456 yield from after .sliding_windows (duration_ , step_ )
14571457
14581458 @overload
1459- def split (self , value : int , unit : str ) -> FlightIterator : ...
1459+ def split (
1460+ self ,
1461+ value : int ,
1462+ unit : str ,
1463+ condition : None | Callable [["Flight" , "Flight" ], bool ] = None ,
1464+ ) -> FlightIterator : ...
14601465
14611466 @overload
1462- def split (self , value : str , unit : None = None ) -> FlightIterator : ...
1467+ def split (
1468+ self ,
1469+ value : str ,
1470+ unit : None = None ,
1471+ condition : None | Callable [["Flight" , "Flight" ], bool ] = None ,
1472+ ) -> FlightIterator : ...
14631473
14641474 @flight_iterator
14651475 def split (
1466- self , value : Union [int , str ] = 10 , unit : Optional [str ] = None
1476+ self ,
1477+ value : Union [int , str ] = 10 ,
1478+ unit : Optional [str ] = None ,
1479+ condition : None | Callable [["Flight" , "Flight" ], bool ] = None ,
14671480 ) -> Iterator ["Flight" ]:
14681481 """Iterates on legs of a Flight based on the distribution of timestamps.
14691482
@@ -1476,13 +1489,51 @@ def split(
14761489 ``np.timedelta64``);
14771490 - in the pandas style: ``Flight.split('10T')`` (see ``pd.Timedelta``)
14781491
1492+ If the `condition` parameter is set, the flight is split between two
1493+ segments only if `condition(f1, f2)` is verified.
1494+
1495+ Example:
1496+
1497+ .. code:: python
1498+
1499+ def no_split_below_5000ft(f1, f2):
1500+ first = f1.data.iloc[-1].altitude >= 5000
1501+ second = f2.data.iloc[0].altitude >= 5000
1502+ return first or second
1503+
1504+ # would yield many segments
1505+ belevingsvlucht.query('altitude > 2000').split('1 min')
1506+
1507+ # yields only one segment
1508+ belevingsvlucht.query('altitude > 2000').split(
1509+ '1 min', condition = no_split_below_5000ft
1510+ )
1511+
14791512 """
14801513 if isinstance (value , int ) and unit is None :
14811514 # default value is 10 m
14821515 unit = "m"
14831516
1484- for data in _split (self .data , value , unit ):
1485- yield self .__class__ (data )
1517+ if condition is None :
1518+ for data in _split (self .data , value , unit ):
1519+ yield self .__class__ (data )
1520+
1521+ else :
1522+ previous = None
1523+ for data in _split (self .data , value , unit ):
1524+ if previous is None :
1525+ previous = self .__class__ (data )
1526+ else :
1527+ latest = self .__class__ (data )
1528+ if condition (previous , latest ):
1529+ yield previous
1530+ previous = latest
1531+ else :
1532+ previous = self .__class__ (
1533+ pd .concat ([previous .data , data ])
1534+ )
1535+ if previous is not None :
1536+ yield previous
14861537
14871538 def max_split (
14881539 self ,
0 commit comments