Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@

from pandas.core.internals import Block

# versioning attribute
_version = "0.15.2"

def _get_pandas_version() -> str:
"""Return the actual installed pandas version for the pandas_version attr."""
from pandas import __version__

return __version__


# encoding
_default_encoding = "UTF-8"
Expand Down Expand Up @@ -2969,9 +2974,12 @@ def version(self) -> tuple[int, int, int]:
"""compute and set our version"""
version = getattr(self.group._v_attrs, "pandas_version", None)
if isinstance(version, str):
version_tup = tuple(int(x) for x in version.split("."))
if len(version_tup) == 2:
version_tup = (*version_tup, 0)
# Tolerate non-numeric trailing components (e.g. "3.0.0.dev0+abc"
# or "2.3.3rc1"); extract up to the first three numeric pieces.
nums = re.findall(r"\d+", version)[:3]
version_tup = tuple(int(x) for x in nums)
if len(version_tup) < 3:
version_tup = (*version_tup, *([0] * (3 - len(version_tup))))
assert len(version_tup) == 3 # needed for mypy
return version_tup
else:
Expand All @@ -2995,7 +3003,7 @@ def __repr__(self) -> str:
def set_object_info(self) -> None:
"""set my pandas type & version"""
self.attrs.pandas_type = str(self.pandas_kind)
self.attrs.pandas_version = str(_version)
self.attrs.pandas_version = _get_pandas_version()

def copy(self) -> Fixed:
new_self = copy.copy(self)
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,10 @@ def test_versioning(temp_hdfstore):
)
store.append("df1", df[:10])
store.append("df1", df[10:])
assert store.root.a._v_attrs.pandas_version == "0.15.2"
assert store.root.b._v_attrs.pandas_version == "0.15.2"
assert store.root.df1._v_attrs.pandas_version == "0.15.2"
# GH#62792 - pandas_version reflects the actual installed pandas version
assert store.root.a._v_attrs.pandas_version == pd.__version__
assert store.root.b._v_attrs.pandas_version == pd.__version__
assert store.root.df1._v_attrs.pandas_version == pd.__version__

# write a file and wipe its versioning
store.append("df2", df)
Expand Down
Loading