Skip to content

Commit 4a1510d

Browse files
committed
ENH: added overwrite parameter to copy (defaults to True)
1 parent 395203f commit 4a1510d

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pandas/io/pytables.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,14 @@ def is_table(self, key):
637637
raise KeyError('No object named %s in the file' % key)
638638
return _is_table_type(group)
639639

640-
def copy(self, file, mode = 'w', propindexes = True, keys = None, complib = None, complevel = None, fletcher32 = False):
640+
def copy(self, file, mode = 'w', propindexes = True, keys = None, complib = None, complevel = None, fletcher32 = False, overwrite = True):
641641
""" copy the existing store to a new file, upgrading in place
642642
643643
Parameters
644644
----------
645645
propindexes: restore indexes in copied file (defaults to True)
646646
keys : list of keys to include in the copy (defaults to all)
647+
overwrite : overwrite (remove and replace) existing nodes in the new store (default is True)
647648
mode, complib, complevel, fletcher32 same as in HDFStore.__init__
648649
649650
Returns
@@ -659,16 +660,22 @@ def copy(self, file, mode = 'w', propindexes = True, keys = None, complib = None
659660
for k in keys:
660661
n = self.get_node(k)
661662
if n is not None:
663+
664+
if k in new_store:
665+
if overwrite:
666+
new_store.remove(k)
667+
662668
data = self.select(k)
663669
if _is_table_type(n):
664670

665671
t = self.get_table(k)
666672
index = False
667673
if propindexes:
668674
index = [ a.name for a in t.axes if a.is_indexed ]
669-
new_store.append(k,data, index=index, data_columns=getattr(t,'data_columns',None))
675+
new_store.append(k,data, index=index, data_columns=getattr(t,'data_columns',None))
670676
else:
671677
new_store.put(k,data)
678+
672679
return new_store
673680

674681
###### private methods ######

pandas/io/tests/test_pytables.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,17 +1737,20 @@ def test_legacy_0_10_read(self):
17371737

17381738
def test_copy(self):
17391739
pth = curpath()
1740-
def do_copy(f = None, keys = None, propindexes = True, **kwargs):
1740+
def do_copy(f = None, new_f = None, keys = None, propindexes = True, **kwargs):
17411741
try:
17421742
import os
17431743

17441744
if f is None:
17451745
f = os.path.join(pth, 'legacy_0.10.h5')
17461746

17471747
store = HDFStore(f, 'r')
1748-
import tempfile
1749-
tmp = tempfile.mkstemp()[1]
1750-
tstore = store.copy(tmp, keys = keys, propindexes = propindexes, **kwargs)
1748+
1749+
if new_f is None:
1750+
import tempfile
1751+
new_f = tempfile.mkstemp()[1]
1752+
1753+
tstore = store.copy(new_f, keys = keys, propindexes = propindexes, **kwargs)
17511754

17521755
# check keys
17531756
if keys is None:
@@ -1771,7 +1774,7 @@ def do_copy(f = None, keys = None, propindexes = True, **kwargs):
17711774
store.close()
17721775
tstore.close()
17731776
import os
1774-
os.remove(tmp)
1777+
os.remove(new_f)
17751778

17761779
do_copy()
17771780
do_copy(keys = ['df'])

0 commit comments

Comments
 (0)