Skip to content

fix: hash datetime64 arrays in object_hash instead of crashing - #61

Merged
MRyutaro merged 1 commit into
mainfrom
fix/60-datetime64-object-hash
Jun 15, 2026
Merged

MRyutaro merged 1 commit into
mainfrom
fix/60-datetime64-object-hash

Conversation

@MRyutaro

Copy link
Copy Markdown
Owner

概要

issue #60 の修正。numpy の datetime64 配列を変数に持つ状態で checkpoint しようとすると、object_hash のフィンガープリント計算が ValueError: cannot include dtype 'M' in a buffer で落ち、チェックポイントの保存・復元が失敗していた問題を修正する。

原因

construct_object_hash は ndarray のバッファ(np.ascontiguousarray(obj.data))をそのまま xxhash に渡していた。datetime64 / timedelta64(dtype kind 'M' / 'm')の配列は buffer プロトコルで公開できないため、ここで例外になっていた。

修正

連続化した配列の uint8 ビュー(arr.view(np.uint8))でrawバイトをハッシュするように変更。これにより datetime64/timedelta64 もハッシュ可能になる。reinterpret できない object 配列は tobytes() にフォールバック。ゼロコピー(連続時)で、既存の数値・構造化・bool 等の配列の挙動は維持。

arr = np.ascontiguousarray(obj)
try:
    buffer = arr.view(np.uint8)
except (ValueError, TypeError):
    buffer = arr.tobytes()
h.update(buffer)

テスト

  • tests/test_object_hash.py に回帰テストを追加:datetime64 / timedelta64 / object 配列がクラッシュせずハッシュでき、内容差を検出できることを検証。
  • 対応ライブラリのチェックポイント往復カバレッジをCIで検証し、READMEに明記する #48 のカバレッジ往復で numpy-datetime64 が Migrate ✅ / Recompute ✅ になることを確認し、KNOWN_LIMITATIONS から除外。カバレッジ表も ✅ に更新(CI が自動同期)。
  • ローカルで全 84 テスト pass(旧 datetime64 の xfailed は解消)、flake8 / mypy クリーン。

補足

  • バグ修正のためコミット種別は fix:(patch バンプ)。

close #60
🤖 Generated with Claude Code

construct_object_hash fed the ndarray's buffer (np.ascontiguousarray(obj.data))
straight to xxhash. datetime64 / timedelta64 ('M'/'m') arrays cannot be exposed
through the buffer protocol, so checkpointing any such variable raised
"cannot include dtype 'M' in a buffer" and aborted the checkpoint.

Hash the raw bytes via a uint8 view of the contiguous array, which works for
those dtypes, with a tobytes() fallback for object-dtype arrays that cannot be
reinterpreted.

Adds object_hash unit tests for datetime64/timedelta64/object arrays, and
removes numpy-datetime64 from the issue #48 coverage KNOWN_LIMITATIONS (the
coverage table now shows it as Migrated/Recomputed).

close #60

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MRyutaro
MRyutaro merged commit ded6738 into main Jun 15, 2026
7 checks passed
@MRyutaro
MRyutaro deleted the fix/60-datetime64-object-hash branch June 15, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime64 配列を checkpoint できない(object_hash がバッファ化不可)

1 participant