pythonintermediate
Grouped Time-Series with ffill
Forward-fill missing time-series values within groups to handle irregular measurement intervals.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
import numpy as np
dates = pd.date_range('2024-01-01', periods=20, freq='D')
df = pd.DataFrame({
'date': dates,
'device': ['A','B'] * 10,
'value': [np.nan if i % 3 == 0 else float(i) for i in range(20)],
})
df = df.sort_values(['device','date'])
df['value_filled'] = df.groupby('device')['value'].transform(lambda s: s.ffill())
print(df)Use Cases
- IoT sensor data
- irregular time-series
- gap filling
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas GroupBy Aggregation Examples
GroupBy operations with multiple aggregations, named aggregations, and transform for DataFrame analysis.
Best for: Sales reporting by region and time period
#pandas#groupby
pythonintermediate
Pandas Time Series Analysis
Time series operations with resampling, rolling windows, date offsets, and period conversions.
Best for: Sales trend analysis with moving averages
#pandas#time-series
pythonintermediate
Pandas Rolling & Expanding Windows
Compute moving averages, rolling sums, and cumulative stats on time-series data with pandas.
Best for: sales forecasting
#pandas#time-series
pythonbeginner
Pandas Time-Series Resampling
Resample time-series data from daily to weekly/monthly frequencies with aggregation functions.
Best for: time-series analytics
#pandas#time-series