import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [np.nan, 2, 3], 'B': [4, np.nan, 6]})
print(df.bfill())
Expected Output:
A B
0 2.0 4.0
1 2.0 6.0
2 3.0 6.0
The DataFrame.bfill() (Backward Fill) method uses the next available valid non-null observation along an axis to fill preceding missing (NaN) values. In time series analysis, backtesting algorithms, and laboratory experiment logging, initial records or preliminary periods often suffer from delayed sensor warm-ups or baseline calibration lags. bfill() looks forward into subsequent index entries to back-propagate valid values to preceding missing cells.
In modern Pandas (2.0+), bfill() replaces the legacy method fillna(method='bfill'), offering enhanced performance and cleaner syntax.
DataFrame.bfill(axis=None, inplace=False, limit=None, downcast=None)axis: 0 or 'index' (fill upwards within columns; default), 1 or 'columns' (fill leftwards across rows).inplace: Boolean (default False). If True, fills values in-place without returning a copy.limit: Integer (default None). Maximum number of consecutive missing values to fill backwards.downcast: Dictionary or string (default None). Dictates dtype downcasting.NaN. bfill() back-fills the initial stable reading to prevent truncating start-of-day data.bfill() propagates official rates back to preceding unassigned transaction records.df.ffill().bfill() ensures complete coverage for time series datasets with missing entries at both leading and trailing edges.Limiting backward propagation prevents future events from unrealistically influencing historical states far in the past.
import pandas as pd
import numpy as np
df_lab = pd.DataFrame({
'READING': [np.nan, np.nan, np.nan, 50.0, 55.0]
})
# Limit backward fill to maximum 2 steps
df_capped = df_lab.bfill(limit=2)
print(df_capped)
Expected Output:
READING
0 NaN
1 50.0
2 50.0
3 50.0
4 55.0
Setting axis=1 propagates valid values from right-to-left across column attributes.
import pandas as pd
import numpy as np
df_targets = pd.DataFrame({
'PRELIM': [np.nan, 10],
'REVIEW': [np.nan, 12],
'FINAL': [25, 15]
})
df_back_row = df_targets.bfill(axis=1)
print(df_back_row)
Expected Output:
PRELIM REVIEW FINAL
0 25.0 25.0 25.0
1 10.0 12.0 15.0
Isolate backward filling within categorical groups to avoid inter-category data corruption.
import pandas as pd
import numpy as np
df_items = pd.DataFrame({
'CAT': ['A', 'A', 'B', 'B'],
'VAL': [np.nan, 100.0, np.nan, 200.0]
})
df_items['VAL'] = df_items.groupby('CAT')['VAL'].bfill()
print(df_items)
bfill() with ffill() to achieve 100% missing data resolution.bfill() on financial or predictive time-series data leaks future observations into historical feature sets.bfill() strictly to static baseline initialization or retroactive compliance reporting.
Q: How do I fill missing values completely across an entire DataFrame?
Chain forward fill and backward fill together: df.ffill().bfill(). This guarantees that missing values at both start and end boundaries are completely populated.
Q: Is bfill() safe for real-time algorithmic trading data?
No. In real-time trading, backward filling introduces look-ahead bias because future prices are not known in real time. Use ffill() for trading backtests.
Explore interactive code cells, test limit parameters, run group-isolated backward fills, and execute self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.