時系列データは他の前処理に加えて、リサンプリング・補間・差分など固有の処理が必要です。
リサンプリング
pandas での粒度変更
Python
# 1 分粒度 → 15 分平均df_15min = df.set_index("timestamp").resample("15min").mean()
# 日次 → 週次合計df_weekly = df.set_index("date").resample("W").sum()
# asfreq vs resample の違い# resample: 集約(mean/sum/last 等)が必要# asfreq: そのまま頻度変更、欠損は NaNdf.asfreq("D") # 日次に揃える、抜けは NaN補間:4 種の使い分け
代表的な補間手法を比較
Python
import pandas as pdimport numpy as np
# 欠損のある時系列を作るts = pd.Series( [10, np.nan, np.nan, 15, np.nan, 22, 25, np.nan, 30], index=pd.date_range("2026-01-01", periods=9, freq="D"))
# 1. Forward fill(直前の値で埋める)── IoT センサー定番print("ffill:", ts.ffill().values)
# 2. 線形補間 ── 数値時系列の標準print("linear:", ts.interpolate(method="linear").values)
# 3. Spline 補間 ── 滑らかさが要る場合print("spline:", ts.interpolate(method="spline", order=2).values)
# 4. 周辺の移動平均で補間def rolling_mean_impute(series, window=3): rolled = series.fillna(series.rolling(window, min_periods=1, center=True).mean()) return rolled.ffill().bfill()
print("rolling_mean:", rolling_mean_impute(ts).values)差分系列:定常性を作る
ARIMA / Prophet 前の定石:株価そのものではなく差分(return)を扱う。
差分・対数差分(金融系の定番)
Python
import pandas as pd
# サンプル:株価のような時系列prices = pd.Series([100, 102, 105, 103, 108, 110, 107])
# 1. 単純差分(前日比)diff = prices.diff()print("差分:", diff.values)
# 2. 変化率pct = prices.pct_change()print("変化率:", pct.values)
# 3. 対数差分(リターンの近似)log_returns = (prices / prices.shift(1)).apply(lambda x: pd.NA if pd.isna(x) else np.log(x))print("対数リターン:", log_returns.values)
# 定常性チェック(ADF 検定)from statsmodels.tsa.stattools import adfullerresult = adfuller(prices.dropna())print(f"原系列 ADF p={result[1]:.3f} (大きい→非定常)")result_diff = adfuller(diff.dropna())print(f"差分系列 ADF p={result_diff[1]:.3f} (小さい→定常)")季節調整(STL 分解)
STL で trend / seasonal / residual に分解
Python
from statsmodels.tsa.seasonal import STLimport matplotlib.pyplot as pltimport japanize_matplotlib # noqa: F401
# 日次の売上時系列(週次季節性あり)を仮定result = STL(daily_sales, period=7).fit()
# 4 つの成分を可視化fig, axes = plt.subplots(4, 1, figsize=(12, 10), sharex=True)result.observed.plot(ax=axes[0]); axes[0].set_title("元データ")result.trend.plot(ax=axes[1]); axes[1].set_title("トレンド")result.seasonal.plot(ax=axes[2]); axes[2].set_title("季節性(曜日依存)")result.resid.plot(ax=axes[3]); axes[3].set_title("残差")plt.tight_layout(); plt.show()
# 季節調整済みデータ = 元 - 季節性deseasonalized = daily_sales - result.seasonalふくふくの進め方
IoT・金融・小売の時系列分析、前処理から予測モデルまで一貫でお手伝いします。
次回予告
EP.14 はテキスト前処理。形態素解析・ストップワード・ 時代の変化。
この記事の感想を教えてください
あなたの 1 クリックで、本当にこの記事は更新されます。「もっと詳しく」「続編希望」が一定数集まった記事は、 ふくふくが 実際に内容を拡充したり続編記事を公開 します。 送信したリアクションはお使いのブラウザに記録され、再カウントされません。