Skip to content

Commit 952a342

Browse files
committed
API: the fmt keyword now replaces the table keyword; allowed values are s|t
the same defaults as prior < 0.13.0 remain, e.g. ``put`` implies 's' (Storer) format and ``append`` imples 't' (Table) format
1 parent a3abf80 commit 952a342

File tree

5 files changed

+157
-42
lines changed

5 files changed

+157
-42
lines changed

doc/source/io.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,7 @@ The examples above show storing using ``put``, which write the HDF5 to ``PyTable
18031803
the ``storer`` format. These types of stores are are **not** appendable once written (though you can simply
18041804
remove them and rewrite). Nor are they **queryable**; they must be
18051805
retrieved in their entirety. These offer very fast writing and slightly faster reading than ``table`` stores.
1806+
This format is specified by default when using ``put`` or by ``fmt='s'``
18061807
18071808
.. warning::
18081809
@@ -1826,7 +1827,7 @@ Table Format
18261827
format. Conceptually a ``table`` is shaped very much like a DataFrame,
18271828
with rows and columns. A ``table`` may be appended to in the same or
18281829
other sessions. In addition, delete & query type operations are
1829-
supported.
1830+
supported. This format is specified by ``fmt='t'`` to ``append`` or ``put``.
18301831
18311832
.. ipython:: python
18321833
:suppress:
@@ -1853,7 +1854,7 @@ supported.
18531854
18541855
.. note::
18551856
1856-
You can also create a ``table`` by passing ``table=True`` to a ``put`` operation.
1857+
You can also create a ``table`` by passing ``fmt='t'`` to a ``put`` operation.
18571858
18581859
.. _io.hdf5-keys:
18591860

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pandas 0.13
9898
- removed the ``warn`` argument from ``open``. Instead a ``PossibleDataLossError`` exception will
9999
be raised if you try to use ``mode='w'`` with an OPEN file handle (:issue:`4367`)
100100
- allow a passed locations array or mask as a ``where`` condition (:issue:`4467`)
101+
- the ``fmt`` keyword now replaces the ``table`` keyword; allowed values are ``s|t``
101102
- ``JSON``
102103

103104
- added ``date_unit`` parameter to specify resolution of timestamps. Options

doc/source/v0.13.0.txt

+20
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,33 @@ API changes
6666
store2.close()
6767
store2
6868

69+
.. ipython:: python
70+
:suppress:
71+
72+
import os
73+
os.remove(path)
74+
6975
- removed the ``_quiet`` attribute, replace by a ``DuplicateWarning`` if retrieving
7076
duplicate rows from a table (:issue:`4367`)
7177
- removed the ``warn`` argument from ``open``. Instead a ``PossibleDataLossError`` exception will
7278
be raised if you try to use ``mode='w'`` with an OPEN file handle (:issue:`4367`)
7379
- allow a passed locations array or mask as a ``where`` condition (:issue:`4467`).
7480
See :ref:`here<io.hdf5-where_mask>` for an example.
7581

82+
- the ``fmt`` keyword now replaces the ``table`` keyword; allowed values are ``s|t``
83+
the same defaults as prior < 0.13.0 remain, e.g. ``put`` implies 's' (Storer) format
84+
and ``append`` imples 't' (Table) format
85+
86+
.. ipython:: python
87+
88+
path = 'test.h5'
89+
df = DataFrame(randn(10,2))
90+
df.to_hdf(path,'df_table',fmt='t')
91+
df.to_hdf(path,'df_table2',append=True)
92+
df.to_hdf(path,'df_storer')
93+
with get_store(path) as store:
94+
print store
95+
7696
.. ipython:: python
7797
:suppress:
7898

pandas/io/pytables.py

+69-17
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ class DuplicateWarning(Warning):
100100
map directly to c-types [inferred_type->%s,key->%s] [items->%s]
101101
"""
102102

103+
# formats
104+
_FORMAT_MAP = {
105+
u('s') : 's',
106+
u('storer') : 's',
107+
u('t') : 't',
108+
u('table') : 't',
109+
}
110+
111+
fmt_deprecate_doc = """
112+
the table keyword has been deprecated
113+
use the fmt='s|t' keyword instead
114+
s : specifies the Storer format
115+
and is the default for put operations
116+
t : specifies the Table format
117+
and is the default for append operations
118+
"""
119+
103120
# map object types
104121
_TYPE_MAP = {
105122

@@ -545,7 +562,7 @@ def select_as_coordinates(self, key, where=None, start=None, stop=None, **kwargs
545562

546563
def unique(self, key, column, **kwargs):
547564
warnings.warn("unique(key,column) is deprecated\n"
548-
"use select_column(key,column).unique() instead")
565+
"use select_column(key,column).unique() instead",FutureWarning)
549566
return self.get_storer(key).read_column(column=column, **kwargs).unique()
550567

551568
def select_column(self, key, column, **kwargs):
@@ -641,24 +658,28 @@ def func(_start, _stop):
641658

642659
return TableIterator(self, func, nrows=nrows, start=start, stop=stop, auto_close=auto_close).get_values()
643660

644-
def put(self, key, value, table=None, append=False, **kwargs):
661+
def put(self, key, value, fmt=None, append=False, **kwargs):
645662
"""
646663
Store object in HDFStore
647664
648665
Parameters
649666
----------
650667
key : object
651668
value : {Series, DataFrame, Panel}
652-
table : boolean, default False
653-
Write as a PyTables Table structure which may perform worse but
654-
allow more flexible operations like searching / selecting subsets
655-
of the data
669+
fmt : 's|t', default is 's' for storer format
670+
s : storer format
671+
Fast writing/reading. Not-appendable, nor searchable
672+
t : table format
673+
Write as a PyTables Table structure which may perform worse but
674+
allow more flexible operations like searching / selecting subsets
675+
of the data
656676
append : boolean, default False
657677
For table data structures, append the input data to the existing
658678
table
659679
encoding : default None, provide an encoding for strings
660680
"""
661-
self._write_to_group(key, value, table=table, append=append, **kwargs)
681+
kwargs = self._validate_format(fmt or 's', kwargs)
682+
self._write_to_group(key, value, append=append, **kwargs)
662683

663684
def remove(self, key, where=None, start=None, stop=None):
664685
"""
@@ -709,7 +730,7 @@ def remove(self, key, where=None, start=None, stop=None):
709730
'can only remove with where on objects written as tables')
710731
return s.delete(where=where, start=start, stop=stop)
711732

712-
def append(self, key, value, columns=None, append=True, **kwargs):
733+
def append(self, key, value, fmt=None, append=True, columns=None, **kwargs):
713734
"""
714735
Append to Table in file. Node must already exist and be Table
715736
format.
@@ -718,6 +739,11 @@ def append(self, key, value, columns=None, append=True, **kwargs):
718739
----------
719740
key : object
720741
value : {Series, DataFrame, Panel, Panel4D}
742+
fmt : 't', default is 't' for table format
743+
t : table format
744+
Write as a PyTables Table structure which may perform worse but
745+
allow more flexible operations like searching / selecting subsets
746+
of the data
721747
append : boolean, default True, append the input data to the existing
722748
data_columns : list of columns to create as data columns, or True to use all columns
723749
min_itemsize : dict of columns that specify minimum string sizes
@@ -735,7 +761,7 @@ def append(self, key, value, columns=None, append=True, **kwargs):
735761
raise Exception(
736762
"columns is not a supported keyword in append, try data_columns")
737763

738-
kwargs['table'] = True
764+
kwargs = self._validate_format(fmt or 't', kwargs)
739765
self._write_to_group(key, value, append=append, **kwargs)
740766

741767
def append_to_multiple(self, d, value, selector, data_columns=None, axes=None, **kwargs):
@@ -901,13 +927,39 @@ def _check_if_open(self):
901927
if not self.is_open:
902928
raise ClosedFileError("{0} file is not open!".format(self._path))
903929

904-
def _create_storer(self, group, value=None, table=False, append=False, **kwargs):
930+
def _validate_format(self, fmt, kwargs):
931+
""" validate / deprecate formats; return the new kwargs """
932+
kwargs = kwargs.copy()
933+
934+
if 'format' in kwargs:
935+
raise TypeError("pls specify an object format with the 'fmt' keyword")
936+
937+
# table arg
938+
table = kwargs.pop('table',None)
939+
940+
if table is not None:
941+
warnings.warn(fmt_deprecate_doc,FutureWarning)
942+
943+
if table:
944+
fmt = 't'
945+
else:
946+
fmt = 's'
947+
948+
# validate
949+
try:
950+
kwargs['fmt'] = _FORMAT_MAP[fmt.lower()]
951+
except:
952+
raise TypeError("invalid HDFStore format specified [{0}]".format(fmt))
953+
954+
return kwargs
955+
956+
def _create_storer(self, group, fmt=None, value=None, append=False, **kwargs):
905957
""" return a suitable Storer class to operate """
906958

907959
def error(t):
908960
raise TypeError(
909-
"cannot properly create the storer for: [%s] [group->%s,value->%s,table->%s,append->%s,kwargs->%s]" %
910-
(t, group, type(value), table, append, kwargs))
961+
"cannot properly create the storer for: [%s] [group->%s,value->%s,fmt->%s,append->%s,kwargs->%s]" %
962+
(t, group, type(value), fmt, append, kwargs))
911963

912964
pt = _ensure_decoded(getattr(group._v_attrs, 'pandas_type', None))
913965
tt = _ensure_decoded(getattr(group._v_attrs, 'table_type', None))
@@ -931,7 +983,7 @@ def error(t):
931983
error('_TYPE_MAP')
932984

933985
# we are actually a table
934-
if table or append:
986+
if fmt == 't':
935987
pt += u('_table')
936988

937989
# a storer node
@@ -983,7 +1035,7 @@ def error(t):
9831035
error('_TABLE_MAP')
9841036

9851037
def _write_to_group(
986-
self, key, value, index=True, table=False, append=False,
1038+
self, key, value, fmt, index=True, append=False,
9871039
complib=None, encoding=None, **kwargs):
9881040
group = self.get_node(key)
9891041

@@ -994,7 +1046,7 @@ def _write_to_group(
9941046

9951047
# we don't want to store a table node at all if are object is 0-len
9961048
# as there are not dtypes
997-
if getattr(value,'empty',None) and (table or append):
1049+
if getattr(value,'empty',None) and (fmt == 't' or append):
9981050
return
9991051

10001052
if group is None:
@@ -1014,12 +1066,12 @@ def _write_to_group(
10141066
group = self._handle.createGroup(path, p)
10151067
path = new_path
10161068

1017-
s = self._create_storer(group, value, table=table, append=append,
1069+
s = self._create_storer(group, fmt, value, append=append,
10181070
encoding=encoding, **kwargs)
10191071
if append:
10201072
# raise if we are trying to append to a non-table,
10211073
# or a table that exists (and we are putting)
1022-
if not s.is_table or (s.is_table and table is None and s.is_exists):
1074+
if not s.is_table or (s.is_table and fmt == 's' and s.is_exists):
10231075
raise ValueError('Can only append to Tables')
10241076
if not s.is_exists:
10251077
s.set_object_info()

0 commit comments

Comments
 (0)