Description
When using Trackintel with the latest Pandas versions, numerous FutureWarnings are displayed concerning inplace operations and downcasting. These warnings appear in specific locations and will lead to actual errors in future Pandas versions.
Warnings observed:
-
Chained Assignment with Inplace Operations:
FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.
The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.
-
Downcasting Deprecation:
FutureWarning: Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated and will change in a future version. Call result.infer_objects(copy=False) instead. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
These warnings appear in these specific locations:
positionfixes.py:361: pfs["tripleg_id"] = pfs["tripleg_id"].ffill()
triplegs.py:274: sp_tpls["is_activity"].fillna(False, inplace=True)
triplegs.py:93: sp_tpls["temp_trip_id"].ffill(inplace=True)
triplegs.py:157: trips_with_act["is_activity"].fillna(False, inplace=True)
Environment
- Python version: 3.11.8
- Pandas version: 2.2.3
- Trackintel version: 1.3.1
Proposed solution
According to Pandas documentation, the recommended solution is to:
- Replace
df[col].method(value, inplace=True) with df.loc[:, col] = df[col].method(value)
- Add
downcast=None to ffill/fillna operations to prevent silent downcasting
Steps to reproduce
The warnings can be reproduced by running any code that calls the generate_staypoints, generate_triplegs, or generate_trips functions from Trackintel with any dataset.
Description
When using Trackintel with the latest Pandas versions, numerous FutureWarnings are displayed concerning inplace operations and downcasting. These warnings appear in specific locations and will lead to actual errors in future Pandas versions.
Warnings observed:
Chained Assignment with Inplace Operations:
Downcasting Deprecation:
These warnings appear in these specific locations:
positionfixes.py:361:pfs["tripleg_id"] = pfs["tripleg_id"].ffill()triplegs.py:274:sp_tpls["is_activity"].fillna(False, inplace=True)triplegs.py:93:sp_tpls["temp_trip_id"].ffill(inplace=True)triplegs.py:157:trips_with_act["is_activity"].fillna(False, inplace=True)Environment
Proposed solution
According to Pandas documentation, the recommended solution is to:
df[col].method(value, inplace=True)withdf.loc[:, col] = df[col].method(value)downcast=Noneto ffill/fillna operations to prevent silent downcastingSteps to reproduce
The warnings can be reproduced by running any code that calls the
generate_staypoints,generate_triplegs, orgenerate_tripsfunctions from Trackintel with any dataset.