BUG: Fix read_hdf failing on generic datetime64 dtype #64006 #64008
+75
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
pd.read_hdfunable to retrieve a pd.Series with dtype as "datetime64" since v3.0.0 #64006 (Replace xxxx with the GitHub issue number)doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.AGENTS.md.Summary of Changes
This PR fixes a regression in read_hdf where reading HDF5 files created by older pandas versions (containing datetime64 metadata without an explicit unit) would raise a TypeError: 'generic' is not a valid TimeUnit.
Implementation Details
Added a check in pandas/io/pytables.py (in both restore_kwargs and read_array) to handle the bare string "datetime64".
If the dtype string is exactly "datetime64" (generic), it now defaults to "datetime64[ns]".
This mirrors the existing legacy handling logic for timedelta64 found immediately adjacent to the fix.
Verification
Since generating "broken" legacy files with modern pandas/numpy is difficult (as they now enforce units), I added a regression test in pandas/tests/io/pytables/test_store.py that uses h5py to manually strip unit metadata from a test file, simulating the legacy format.
Tests Added:
test_read_hdf_datetime64_without_unit_gh64006: Verifies that generic datetime64 metadata is correctly read as [ns].
test_read_hdf_preserves_explicit_units: Verifies that files with explicit units (e.g., [s], [ms]) are not overwritten by this fix.