Skip to content

Commit 18654d9

Browse files
committed
Make it tables < 3.4.3 friendly
1 parent 6dd43fe commit 18654d9

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

pandas/io/pytables.py

+14-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,18 @@ 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+
4151+
if tables_version >= "3.4.3":
4152+
options["track_times"] = track_times
4153+
else:
4154+
raise ValueError(
4155+
"You cannot set track_times with table version < 3.4.3"
4156+
)
4157+
41484158
# create the table
4149-
table._handle.create_table(table.group, track_times=track_times, **options)
4159+
table._handle.create_table(table.group, **options)
41504160

41514161
# update my info
41524162
table.attrs.info = table.info

pandas/tests/io/pytables/test_store.py

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

304304
import hashlib
305305
import time
306+
import tables
306307

307308
def checksum(filename, hash_factory=hashlib.md5, chunk_num_blocks=128):
308309
h = hash_factory()
@@ -315,32 +316,39 @@ def create_h5_and_return_checksum(track_times):
315316
with ensure_clean_path(setup_path) as path:
316317
df = pd.DataFrame({"a": [1]})
317318

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()
319+
with pd.HDFStore(path, mode="w") as hdf:
320+
hdf.put(
321+
"table",
322+
df,
323+
format="table",
324+
data_columns=True,
325+
index=None,
326+
track_times=track_times,
327+
)
328+
328329
return checksum(path)
329330

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)
331+
if tables.__version__ < "3.4.3":
332+
with pytest.raises(
333+
ValueError,
334+
match="You cannot set track_times with table version < 3.4.3",
335+
):
336+
create_h5_and_return_checksum(track_times=False)
337+
else:
338+
checksum_0_tt_false = create_h5_and_return_checksum(track_times=False)
339+
checksum_0_tt_true = create_h5_and_return_checksum(track_times=True)
332340

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

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)
344+
checksum_1_tt_false = create_h5_and_return_checksum(track_times=False)
345+
checksum_1_tt_true = create_h5_and_return_checksum(track_times=True)
338346

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

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

345353
def test_keys_ignore_hdf_softlink(self, setup_path):
346354

0 commit comments

Comments
 (0)