Cleaning Messy Export Files Before You Analyse Them
In short This article catalogues the specific corruptions in UK energy exports — DST duplicate hours, missing half-hours, cumulative-vs-interval confusion, meter exchanges, timezone drift — and gives validation checks that catch each before they poison your model.
You download a year of half-hourly data from your supplier portal, paste it into a spreadsheet or hand it to an AI tool, and ask what a 5 kWh battery would save you. The answer comes back confident, to the penny. It is also wrong, because the file was quietly broken in four places and nobody told the model.
If you want to clean smart meter csv data properly, you need to know the specific ways UK exports go bad. They are not random. The same handful of faults turns up again and again, and each one has a cheap check that catches it. This post catalogues them, with the checks, so you can run them before any modelling starts.
A sanity baseline first. A normal year of half-hourly data has 17,520 rows (365 x 48). A leap year has 17,568. Write that number down. Half of what follows is just checking whether you have it.
1. Daylight saving: the 23-hour and 25-hour days
The UK clocks change twice a year, and exports handle it inconsistently.
- Spring (last Sunday of March): the day has 46 half-hours. 01:00 to 02:00 local time does not exist.
- Autumn (last Sunday of October): the day has 50 half-hours. 01:00 to 02:00 happens twice.
Exports that use local time with no offset produce duplicate timestamps in autumn and a gap in spring. Pandas will then either refuse to build a unique index or, worse, silently let you pivot and average the two “01:30” rows together.
Here is what the autumn 2025 problem looks like in a local-time export:
timestamp,kwh
2025-10-26 00:30,0.21
2025-10-26 01:00,0.19
2025-10-26 01:30,0.18
2025-10-26 01:00,0.20
2025-10-26 01:30,0.22
2025-10-26 02:00,0.17
Same labels, different physical half-hours. The check:
import pandas as pd
df = pd.read_csv("export.csv")
dupes = df[df.duplicated("timestamp", keep=False)]
print(dupes["timestamp"].str[:10].value_counts())
Expect exactly 2 rows per duplicated label, on one date in late October only, 2 half-hours (01:00 and 01:30) giving 4 rows. If duplicates appear on other dates, that is a different fault (usually a double-pasted download).
The fix: localise properly. pd.to_datetime(df.timestamp).dt.tz_localize("Europe/London", ambiguous="infer") handles autumn when rows are in order. Then convert to UTC and work in UTC for everything. Convert back to local time only for charts.
2. Missing half-hours
Smart meters miss readings. The DCC network drops comms, the in-home display loses its link, the supplier’s own system has a hiccup. A typical domestic year might lose 20 to 150 half-hours, scattered, or sometimes one block of several days.
Missing data matters more than it looks. If you sum a column with 300 rows missing, your annual total is low by roughly 1.7% before anything else goes wrong, and your tariff comparison inherits that.
The check is to reindex against a complete calendar and count what falls out:
s = df.set_index("ts_utc")["kwh"]
full = pd.date_range(s.index.min(), s.index.max(), freq="30min", tz="UTC")
missing = full.difference(s.index)
print(len(full), len(s), len(missing))
Sample output for a real-looking file:
17520 17391 129
Then group the gaps. 129 missing could be 129 singletons (harmless, interpolate) or one 5.4-day outage (not harmless, because interpolating across a cold week invents consumption). My rule: interpolate runs of up to 4 half-hours (2 hours), and for anything longer, flag the days and exclude them from the comparison, rather than filling them with fiction.
Also watch for gaps that were already filled by the supplier with zeros. A run of exactly 0.000 for 48 rows in a house with a fridge is a gap wearing a disguise. Check: (s == 0).astype(int).groupby((s != 0).cumsum()).sum().max(). Anything over about 6 consecutive zeros deserves a look.
3. Cumulative versus interval readings
This is the one that does the most damage, because the file looks perfectly fine.
Some exports (Octopus’s API consumption endpoint, Hildebrand Glow, many DCC-derived downloads) give interval values: kWh used in that half-hour. Others give the register reading: the odometer-style total, such as 18,342.7 kWh. Home Assistant energy sensors and some inverter exports (SolarEdge, Growatt) are also cumulative.
Feed a register column to a model as if it were interval data and your “annual consumption” is 18 million kWh. That one is at least obvious. The subtle version is a file that mixes both, or a solar generation column that is cumulative while the import column is interval.
Checks, in order of cheapness:
- Is the column monotonic?
s.is_monotonic_increasingreturningTrueover a year means cumulative. Interval data goes up and down. - Is the maximum implausible? A UK home drawing more than 15 kWh in a half-hour (30 kW average) is not credible unless you have an EV charger and a heat pump both flat out, and even then you cap out near 12.
- Is the median enormous? Median over 100 on a half-hourly kWh column is a register.
To convert, difference it:
interval = s.diff()
print(interval.describe())
print((interval < 0).sum()) # should be 0, see next section
Cumulative solar generation has a second trap: it often resets to zero at midnight (daily yield counters), so a plain diff() gives a large negative at 00:00 each day. Treat negatives there as a reset and use the new value itself as the interval, not the difference.
4. Meter exchanges
If your meter was swapped (the old dumb one for a smart one, or one smart meter for a replacement under a supplier switch), the register restarts. Then diff() produces a single huge negative, or, if the new meter reads higher than the old, a single huge positive.
Example around a swap:
2024-06-11 09:00 48211.4
2024-06-11 09:30 48211.9
2024-06-11 10:00 0.0
2024-06-11 10:30 0.3
diff() gives -48211.9 at 10:00. Left alone, that row wipes 48 MWh off your annual total.
Check: any negative interval from a monotonic register is either a reset or a meter exchange. List them: interval[interval < 0]. For each one, set that single interval to NaN (the true usage in that half-hour is unknowable) and treat it as a gap under section 2. Do not set it to zero and do not take the absolute value.
Also check your supplier switches. Two exports from two suppliers stitched together will often overlap by a day or leave a day-long hole. Compare the row count in each file’s date range against 48 x days.
5. Timezone drift
This is the sneaky one. Your data is all there, with no duplicates and no gaps, but it is shifted by an hour for half the year.
It happens when a tool writes UTC with a trailing “Z”, another tool strips the “Z” and re-reads it as local time, and BST months come out an hour early. Or when you merge your import data (UTC) with solar generation from a forecast API such as Open-Meteo (local time by default, unless you ask for timezone=UTC).
You can detect it with physics. Solar generation should peak near solar noon, which in the UK is about 12:00 to 13:15 UTC depending on longitude, and at the same clock time for local winter. In BST that is 13:00 to 14:15 local.
gen = solar.groupby(solar.index.hour).mean()
print(gen.idxmax())
For a south-facing array in Leeds in June, working in UTC, you expect a peak hour of 11 or 12. If you see 13 or 14 in UTC, you are carrying a BST offset you did not mean to. Run it separately for a June month and a December month: the peak hour should be the same in UTC in both. If it jumps by one between them, you have drift.
Overnight base load works too. Your lowest consumption window (fridge, router, standby) is usually 02:00 to 05:00. If it slides an hour at the clock change, so does your data.
A validation script worth keeping
Rather than remembering all this, put the checks in one function and run it on every file you import. A sensible version asserts:
| Check | Pass condition |
|---|---|
| Row count after UTC conversion | Equals 48 x days in range |
| Duplicate UTC timestamps | 0 |
| Longest gap | Under 4 half-hours, or flagged |
| Negative intervals | 0 after reset handling |
| Max half-hourly kWh | Under 15 for import |
| Annual total | Within 20% of the sum of your last 12 bills |
| Solar peak hour (UTC) | Same in June and December |
That bill comparison is the cheapest overall test. Your supplier’s annual kWh figure is on the statement. If your cleaned file differs by more than about 5%, something is still wrong. Past 20%, stop.
You can also have an AI assistant write this function for you, and the prompts and pipeline patterns for doing that are in AI Tools, Prompts and Data Pipelines. Give it your real column names and ask it to fail loudly, not to fix things silently. A cleaner that quietly repairs problems is the same hazard as a model that quietly ignores them.
Do the cleaning once, save the result as a new file next to the original, and never edit the raw export. When your battery payback figure later looks strange, you will want to be able to say exactly what you changed.