diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 03da842157e1e..04a7089b95230 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -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" @@ -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: @@ -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) diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 840a1088eda0e..48b0c8a4e1375 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -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)