Python for Test Data: pandas, Resampling, Alignment, SQLite and Plots
Spreadsheets struggle beyond a few hundred thousand rows; test rigs produce millions. A few lines of Python with pandas read the logs, fix timestamps, resample to a common rate, align process and vibration data, store them in a database and plot trends. The same script then runs on every new file, which is the real gain.
Key takeaways
- Read CSVs with pandas.read_csv, parse the time column, and set it as the index.
- Resample to a common interval (df.resample('1s').mean()) and use rolling windows for moving averages and limits.
- Align data from two systems with pandas.merge_asof on timestamps, with a tolerance.
- Read huge files in chunks and append results to SQLite or Parquet instead of loading everything into memory.
- Memory needed ≈ rows × numeric columns × 8 bytes; one million rows × 20 columns ≈ 160 MB.
1. Reading and cleaning
import pandas as pd
df = pd.read_csv("run_2026-11-24.csv", parse_dates=["Time"], dayfirst=True)
df = df.set_index("Time").sort_index()
df = df[~df.index.duplicated()] # drop duplicate timestamps
df = df.apply(pd.to_numeric, errors="coerce") # text like "---" becomes NaN
print(df.describe())
2. Resampling and rolling statistics
one_s = df.resample("1s").mean() # 1-second means
one_min = df.resample("1min").agg(["mean", "max"])
one_s["Temp_roll"] = one_s["Oil_Temp"].rolling("10min").mean()
alarms = one_s[one_s["Vib_RMS"] > 2.5] # rows above a limit
3. Aligning two systems
proc = pd.read_csv("process.csv", parse_dates=["Time"]).sort_values("Time")
vib = pd.read_csv("vibration_features.csv", parse_dates=["Time"]).sort_values("Time")
both = pd.merge_asof(vib, proc, on="Time", direction="nearest", tolerance=pd.Timedelta("1s"))
4. Large files and storage
import sqlite3
con = sqlite3.connect("test_data.db")
for chunk in pd.read_csv("huge_log.csv", parse_dates=["Time"], chunksize=500_000):
chunk.resample("1s", on="Time").mean().to_sql("features_1s", con, if_exists="append")
q = pd.read_sql("SELECT * FROM features_1s WHERE Time >= '2026-11-24'", con, parse_dates=["Time"])
df.to_parquet("run.parquet") # compact, fast, keeps types (needs pyarrow)
5. Spectrum and plot
import numpy as np, matplotlib.pyplot as plt
x = raw["Acc_X"].to_numpy(); fs = 25600
w = np.hanning(len(x)); X = np.abs(np.fft.rfft(x * w)) * 2 / w.sum()
f = np.fft.rfftfreq(len(x), 1 / fs)
fig, ax = plt.subplots(2, 1, figsize=(12, 6))
one_min["Vib_RMS"]["mean"].plot(ax=ax[0]); ax[0].set_ylabel("mm/s")
ax[1].plot(f, X); ax[1].set_xlim(0, 2000); ax[1].set_xlabel("Hz")
fig.tight_layout(); fig.savefig("report.png", dpi=150)
6. Calculators
Snippet builder and memory estimate
7. Self-test: check your understanding
Pick an answer to see the explanation. Numerical answers are accepted within the stated tolerance (±3 % unless noted).
Score
8. References and further reading
- McKinney, W., Python for Data Analysis, 3rd ed., O'Reilly, 2022.
- pandas documentation (read_csv, resample, merge_asof, to_sql).
- Python standard library documentation: sqlite3.
- NumPy documentation: numpy.fft.