Skip to content

Commit 88eea0e

Browse files
committed
Make it tables < 3.4.3 friendly
1 parent 6dd43fe commit 88eea0e

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

pandas/io/pytables.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def put(
984984
data_columns: Optional[List[str]] = None,
985985
encoding=None,
986986
errors: str = "strict",
987-
track_times: bool = True,
987+
track_times: Optional[bool] = None,
988988
):
989989
"""
990990
Store object in HDFStore.
@@ -1630,7 +1630,7 @@ def _write_to_group(
16301630
data_columns=None,
16311631
encoding=None,
16321632
errors: str = "strict",
1633-
track_times: bool = True,
1633+
track_times: Optional[bool] = None,
16341634
):
16351635
group = self.get_node(key)
16361636

@@ -4113,7 +4113,7 @@ def write(
41134113
dropna=False,
41144114
nan_rep=None,
41154115
data_columns=None,
4116-
track_times=True,
4116+
track_times=None,
41174117
):
41184118

41194119
if not append and self.is_exists:
@@ -4145,8 +4145,19 @@ def write(
41454145
# set the table attributes
41464146
table.set_attrs()
41474147

4148+
if track_times is not None:
4149+
from tables import __version__ as tables_version
4150+
from packaging.version import Version
4151+
4152+
if Version(tables_version) >= Version("3.4.3"):
4153+
options["track_times"] = track_times
4154+
else:
4155+
raise ValueError(
4156+
"You cannot set track_times with table version < 3.4.3"
4157+
)
4158+
41484159
# create the table
4149-
table._handle.create_table(table.group, track_times=track_times, **options)
4160+
table._handle.create_table(table.group, **options)
41504161

41514162
# update my info
41524163
table.attrs.info = table.info

pandas/tests/io/pytables/test_store.py

+29-20
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ def test_no_track_times(self, setup_path):
303303

304304
import hashlib
305305
import time
306+
import tables
307+
from packaging.version import Version
306308

307309
def checksum(filename, hash_factory=hashlib.md5, chunk_num_blocks=128):
308310
h = hash_factory()
@@ -315,32 +317,39 @@ def create_h5_and_return_checksum(track_times):
315317
with ensure_clean_path(setup_path) as path:
316318
df = pd.DataFrame({"a": [1]})
317319

318-
hdf = pd.HDFStore(path, mode="w")
319-
hdf.put(
320-
"table",
321-
df,
322-
format="table",
323-
data_columns=True,
324-
index=None,
325-
track_times=track_times,
326-
)
327-
hdf.close()
320+
with pd.HDFStore(path, mode="w") as hdf:
321+
hdf.put(
322+
"table",
323+
df,
324+
format="table",
325+
data_columns=True,
326+
index=None,
327+
track_times=track_times,
328+
)
329+
328330
return checksum(path)
329331

330-
checksum_0_tt_false = create_h5_and_return_checksum(track_times=False)
331-
checksum_0_tt_true = create_h5_and_return_checksum(track_times=True)
332+
if Version(tables.__version__) < Version("3.4.3"):
333+
with pytest.raises(
334+
ValueError,
335+
match="You cannot set track_times with table version < 3.4.3",
336+
):
337+
create_h5_and_return_checksum(track_times=False)
338+
else:
339+
checksum_0_tt_false = create_h5_and_return_checksum(track_times=False)
340+
checksum_0_tt_true = create_h5_and_return_checksum(track_times=True)
332341

333-
# sleep is necessary to create h5 with different creation time
334-
time.sleep(1)
342+
# sleep is necessary to create h5 with different creation time
343+
time.sleep(1)
335344

336-
checksum_1_tt_false = create_h5_and_return_checksum(track_times=False)
337-
checksum_1_tt_true = create_h5_and_return_checksum(track_times=True)
345+
checksum_1_tt_false = create_h5_and_return_checksum(track_times=False)
346+
checksum_1_tt_true = create_h5_and_return_checksum(track_times=True)
338347

339-
# checksums are the same if track_time = False
340-
assert checksum_0_tt_false == checksum_1_tt_false
348+
# checksums are the same if track_time = False
349+
assert checksum_0_tt_false == checksum_1_tt_false
341350

342-
# checksums are NOT same if track_time = True
343-
assert checksum_0_tt_true != checksum_1_tt_true
351+
# checksums are NOT same if track_time = True
352+
assert checksum_0_tt_true != checksum_1_tt_true
344353

345354
def test_keys_ignore_hdf_softlink(self, setup_path):
346355

0 commit comments

Comments
 (0)