Time Series Analysis
You have one row per event and you want one row per month: the total value, the number of events, and a two-month moving average to smooth the trend. Ciaren builds the monthly rows by extracting the year and month from the timestamp and grouping on them, then adds the moving average with Rolling Aggregate.
You'll use: File Input → Change Types → Extract Date Parts → Group By Aggregate → Rename Columns → Sort Rows → Rolling Aggregate → File Output.
- InputFile Inputevents.csv
- CleanChange Typesoccurred_at →
datetime · value → float - TransformExtract Date Partsadds occurred_at_year, occurred_at_month
- TransformGroup By Aggregateyear + month · sum(value) · count(event_id)
- CleanRename Columnsvalue →
total_value · event_id → num_events - CleanSort Rowsyear, month ascending
- TransformRolling Aggregatemean of total_value over 2 rows
- OutputFile Outputmonthly_summary.csv
Sample data
Save this as events.csv, or download events.csv,
and upload it on the Datasets page:
event_id,occurred_at,value
1,2024-01-05,10
2,2024-01-20,14
3,2024-02-02,9
4,2024-02-18,21
5,2024-03-01,17
6,2024-03-29,13Six events, two in each of January, February and March 2024.
Build the flow
- File Input: File type CSV, dataset
events.csv. - Change Types: under Casts, set
occurred_at→datetimeandvalue→float. Extract Date Parts can parse date strings itself, but a real datetime column fails loudly on a malformed date here instead of producing an empty month later. - Extract Date Parts: Date column
occurred_at, Parts to extractyearandmonth. This addsoccurred_at_yearandoccurred_at_month. - Group By Aggregate: Group by columns
occurred_at_year,occurred_at_month. Under Aggregations, setvalue→sumandevent_id→count. Group on both parts: grouping on month alone would merge January 2024 with January 2025. - Rename Columns:
value→total_value,event_id→num_events. - Sort Rows: Sort by columns
occurred_at_year,occurred_at_month, Order Ascending. - Rolling Aggregate: Target column
total_value, Function Mean (moving average), Window (rows)2, Order byoccurred_at_year,occurred_at_month, New columnavg_2_months. Leave Min periods and Partition by empty. - File Output: File type CSV, Dataset name
monthly_summary.
Prefer to describe it?
With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "Parse occurred_at as a date, total value and count event_id per month, then add a 2-month rolling average of the monthly totals." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.
How the rolling window moves
Rolling Aggregate orders the rows by Order by, then, for each row, takes the mean of that row and the row before it. The window is counted in rows, not in calendar time, so here two rows means two months only because there is exactly one row per month.
January gets null because its window is not full: there is no December row. Set
Min periods to 1 if you want January to show 24.0, the mean of the one row
it has.
Result
| event_id | occurred_at | value |
|---|---|---|
| 1 | 2024-01-05 | 10 |
| 2 | 2024-01-20 | 14 |
| 3 | 2024-02-02 | 9 |
| 4 | 2024-02-18 | 21 |
| 5 | 2024-03-01 | 17 |
| 6 | 2024-03-29 | 13 |
| occurred_at_year | occurred_at_month | total_valuenew | num_eventsnew | avg_2_monthsnew |
|---|---|---|---|---|
| 2024 | 1 | 24 | 2 | null |
| 2024 | 2 | 30 | 2 | 27 |
| 2024 | 3 | 30 | 2 | 30 |
Check the result
- Three rows, one per month, in calendar order.
num_eventsis 2 in every month and adds up to 6, the number of events.total_valueadds up to 84, the sum of the six values.avg_2_monthsis empty for January, 27.0 for February and 30.0 for March.
Exported Python
Click Export in the editor toolbar and open the pandas tab:
import pandas as pd
df_events = pd.read_csv('events.csv')
df_events = df_events.assign(occurred_at=lambda _d: pd.to_datetime(_d['occurred_at']), value=lambda _d: _d['value'].astype('float64'))
_dt = pd.to_datetime(df_events['occurred_at'], errors='coerce')
df_events = df_events.assign(occurred_at_year=_dt.dt.year, occurred_at_month=_dt.dt.month)
df_events = (
df_events.groupby(['occurred_at_year', 'occurred_at_month'], dropna=False)
.agg({'value': 'sum', 'event_id': 'count'})
.reset_index()
.rename(columns={'value': 'total_value', 'event_id': 'num_events'})
.sort_values(['occurred_at_year', 'occurred_at_month'])
.assign(avg_2_months=lambda _d: _d.sort_values(['occurred_at_year', 'occurred_at_month'], kind='stable')['total_value'].rolling(2).mean())
)
df_events.to_csv('monthly_summary.csv', index=False)Run it next to events.csv and monthly_summary.csv matches the result table,
with an empty cell for January's average. The rolling step sorts by its own
Order by columns, so it gives the same averages even without the Sort Rows
node; Sort Rows is there for the order of the output file.
Pitfalls
- A month with no events has no row. Group By Aggregate only creates rows for months that appear in the data. If April had no events, the window for May would average May with March.
- Windows are rows, not days. On event-level data, a window of 7 is seven events, not seven days. Group to one row per day or month first.
- Set Order by. Without it, the window follows the incoming row order, which may not be chronological.
Variations
- Day-of-week pattern: pick
weekdayin Extract Date Parts and group byoccurred_at_weekdayinstead. - Change from the previous month: use Row Difference on
total_value, ordered by year and month. - Separate series per group: set Partition by in Rolling Aggregate (for
example a
regioncolumn) so each group gets its own moving average.