Gallery · In-memory analytics

Ninety days of a grid, held in memory

Ninety days of a power grid through Datafication's DataBlock: computed columns, a grouped aggregate, and a 24-hour moving average, all of it C# over data already in memory. ScottPlot draws the result in the same cell that computed it.

datablock-analytics.verso C# · 16 cells Read-only preview Download

Ninety days of a grid, held in memory

A DataBlock is an in-memory analytical database. Not a list you loop over, and not a handle on a server somewhere: the columns live in this process, and the operations on them are the ones you would reach for in SQL. Filters, computed columns, grouped aggregates, window functions.

The data is 2,160 hourly readings from a small regional grid, one row per hour for ninety days, in megawatts. Two questions, the ones anybody asks of a generation log: when in the day do renewables carry the most, and does anything move across the season.

Every cell here is C#, including the charts.

[1]
codeC#
1#r "nuget: Datafication.Core, 1.0.11"
2#r "nuget: Datafication.CsvConnector, 1.0.11"
3#r "nuget: ScottPlot, 5.1.59"
✓completed
Installed Packages
  • Datafication.Core, 1.0.11
  • Datafication.CsvConnector, 1.0.11
  • ScottPlot, 5.1.59
Transitive dependencies (9)
  • HarfBuzzSharp, 8.3.1.1
  • HarfBuzzSharp.NativeAssets.Linux, 8.3.1.1
  • HarfBuzzSharp.NativeAssets.macOS, 8.3.1.1
  • HarfBuzzSharp.NativeAssets.Win32, 8.3.1.1
  • SkiaSharp, 3.119.0
  • SkiaSharp.HarfBuzz, 3.119.0
  • SkiaSharp.NativeAssets.Linux.NoDependencies, 3.119.0
  • SkiaSharp.NativeAssets.macOS, 3.119.0
  • SkiaSharp.NativeAssets.Win32, 3.119.0

Reading the file

A connector turns a source into a DataBlock. Source is a Uri, and a relative one is resolved against the host process rather than against this file, so the path is made absolute first. Keep grid-hourly.csv beside the notebook and run from that folder.

A DataBlock is columnar, so looking at it means projecting a few rows back out. Preview below does that into a DataTable, which Verso already knows how to render as a table.

[2]
codeC#
1using System.Data;
2using Datafication.Core.Data;
3using Datafication.Connectors.CsvConnector;
4
5var connector = new CsvDataConnector(new CsvConnectorConfiguration
6{
7 Source = new Uri(Path.GetFullPath("grid-hourly.csv")),
8 HeaderRow = true
9});
10
11var grid = await connector.GetDataAsync();
12
13DataTable Preview(DataBlock block, int rows = 8)
14{
15 var names = block.Schema.GetColumnNames().ToArray();
16 var table = new DataTable();
17
18 foreach (var name in names)
19 {
20 var sample = Enumerable.Range(0, block.RowCount)
21 .Select(row => block[row, name])
22 .FirstOrDefault(value => value is not null);
23
24 table.Columns.Add(name, sample?.GetType() ?? typeof(string));
25 }
26
27 foreach (var row in Enumerable.Range(0, Math.Min(rows, block.RowCount)))
28 table.Rows.Add(names.Select(name => block[row, name]).ToArray());
29
30 return table;
31}
32
33Preview(grid, 5)
✓completed
hourwindsolarhydrogascoal
3/1/2026 12:00:00 AM582.20279.4965.3398.9
3/1/2026 1:00:00 AM532.50247.7879.2319.3
3/1/2026 2:00:00 AM585.30263864.2353
3/1/2026 3:00:00 AM600.70273.3830.4325.6
3/1/2026 4:00:00 AM6030266.3886.9339.4

Columns that did not exist a moment ago

Compute takes an expression over the columns already present and leaves a new one behind. The expressions are strings, evaluated against the block, and they can reach for dates as easily as arithmetic: HOUR(hour) pulls the hour of the day out of a timestamp the connector inferred from text.

Each call returns a new block, so the chain reads top to bottom and nothing is mutated along the way.

[3]
codeC#
1var hourly = grid
2 .Compute("renewable", "wind + solar + hydro")
3 .Compute("total", "wind + solar + hydro + gas + coal")
4 .Compute("renewable_share", "renewable / total")
5 .Compute("share_pct", "ROUND(renewable_share * 100, 1)")
6 .Compute("hour_of_day", "HOUR(hour)");
7
8Preview(hourly.Select("hour", "hour_of_day", "wind", "solar", "share_pct"), 5)
✓completed
hourhour_of_daywindsolarshare_pct
3/1/2026 12:00:00 AM0582.2038.7
3/1/2026 1:00:00 AM1532.5039.4
3/1/2026 2:00:00 AM2585.3041.1
3/1/2026 3:00:00 AM3600.7043.1
3/1/2026 4:00:00 AM4603041.5

The first question: when in the day

Grouping by the hour of the day and averaging the share across all ninety days collapses 2,160 rows into 24. GroupByAggregate returns them in whatever order the grouping produced, so the sort is not decoration: without it the line chart below would zigzag across the plot.

[4]
codeC#
1var daily = hourly
2 .GroupByAggregate("hour_of_day", "renewable_share", AggregationType.Mean, "avg_share")
3 .Compute("percent", "ROUND(avg_share * 100, 1)")
4 .Sort(SortDirection.Ascending, "hour_of_day");
5
6Preview(daily.Select("hour_of_day", "percent"), 24)
✓completed
hour_of_daypercent
041.3
141.7
242.2
342.4
443
545.8
647.4
750.4
854.1
957.5
1058.8
1159.4
1258.8
1357.5
1455.5
1551.8
1646.6
1740.6
1835.1
1932.2
2031.4
2133.9
2236.5
2338.9

The shape is the one you would hope for and the one that makes solar awkward. Renewables carry their largest share around the middle of the day and their smallest in the early evening, which is exactly when demand is highest.

[5]
codeC#
1using ScottPlot;
2
3double[] Column(DataBlock block, string name) =>
4 Enumerable.Range(0, block.RowCount)
5 .Select(row => Convert.ToDouble(block[row, name]))
6 .ToArray();
7
8var profile = new Plot();
9var line = profile.Add.Scatter(Column(daily, "hour_of_day"), Column(daily, "percent"));
10line.LineWidth = 2.5f;
11line.MarkerSize = 6;
12
13profile.Title("Renewable share of generation, by hour of day");
14profile.XLabel("Hour");
15profile.YLabel("Share of generation (%)");
16profile.Axes.SetLimitsX(0, 23);
17
18profile.ScaleFactor = 2;
19profile.GetImageBytes(1880, 760, ImageFormat.Png)
✓completed

The second question: across the season

An hourly series is too noisy to read a trend from, and this one swings between 15% and 76% depending on whether the wind is blowing. A window function is the usual answer: MovingAverage over the previous 24 hours, ordered by timestamp, which flattens the day-night cycle and leaves the weather behind it.

Window adds the result as a new column, so the smoothed series sits beside the raw one and both can be drawn together.

[6]
codeC#
1var smoothed = hourly
2 .Window(
3 "renewable_share",
4 WindowFunctionType.MovingAverage,
5 windowSize: 24,
6 resultColumnName: "rolling_day",
7 orderByColumn: "hour")
8 .Compute("rolling_pct", "ROUND(rolling_day * 100, 1)");
9
10Preview(smoothed.Select("hour", "share_pct", "rolling_pct"), 5)
✓completed
hourshare_pctrolling_pct
3/1/2026 12:00:00 AM38.738.7
3/1/2026 1:00:00 AM39.439.1
3/1/2026 2:00:00 AM41.139.7
3/1/2026 3:00:00 AM43.140.6
3/1/2026 4:00:00 AM41.540.7
[7]
codeC#
1// The timestamps go on the axis as real dates rather than as a row counter.
2var stamps = Enumerable.Range(0, smoothed.RowCount)
3 .Select(row => Convert.ToDateTime(smoothed[row, "hour"]).ToOADate())
4 .ToArray();
5
6var season = new Plot();
7
8var noise = season.Add.Scatter(stamps, Column(smoothed, "share_pct"));
9noise.LineWidth = 1;
10noise.MarkerSize = 0;
11noise.Color = Colors.Gray.WithAlpha(0.35);
12
13var trend = season.Add.Scatter(stamps, Column(smoothed, "rolling_pct"));
14trend.LineWidth = 2.5f;
15trend.MarkerSize = 0;
16
17season.Title("Renewable share over ninety days: hourly (grey) and 24-hour average (orange)");
18season.YLabel("Share of generation (%)");
19season.Axes.DateTimeTicksBottom();
20
21season.ScaleFactor = 2;
22season.GetImageBytes(1880, 760, ImageFormat.Png)
✓completed

What the block is doing

Nothing above opened a connection or wrote a temporary file. The CSV was read once, and every operation after that was a column transform over data already in memory: five computed columns, one grouped aggregate, one 24-hour window, two projections. The last cell asks the block for its own summary.

[8]
codeC#
1Preview(grid.Info(), 10)
✓completed
ColumnLabelTypeNon-Null CountNull Count
hourhourDateTime21600
windwindDouble21600
solarsolarDouble21600
hydrohydroDouble21600
gasgasDouble21600
coalcoalDouble21600

Take it home

Download this notebook and grid-hourly.csv from the gallery page, put them in the same folder, then open it in VS Code with the Verso extension or serve it with verso serve datablock-analytics.verso. Change the window from 24 hours to a week and rerun; the second chart is the only thing that moves.

Run it yourself

Nothing on this page is executable; the outputs were baked in by a real run. Download the notebook and it runs anywhere Verso does: VS Code, the browser, or the terminal.

dotnet tool install --global Verso.Cli
verso serve datablock-analytics.verso

This notebook reads grid-hourly.csv; download it into the same folder as the notebook.

What this shows

Ninety days of a power grid through Datafication's DataBlock: computed columns, a grouped aggregate, and a 24-hour moving average, all of it C# over data already in memory. ScottPlot draws the result in the same cell that computed it.

Every language in this notebook reads and writes the same variable store, so a value produced in one cell is simply there in the next, whatever the language.