Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: explicit signature for to_hdf #29939

Merged
merged 6 commits into from
Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,19 @@ def to_json(
indent=indent,
)

def to_hdf(self, path_or_buf, key, **kwargs):
def to_hdf(
self,
path_or_buf,
key: str,
mode: str = "a",
complevel: Optional[int] = None,
complib: Optional[str] = None,
append: bool_t = False,
format: Optional[str] = None,
errors: str = "strict",
encoding: str = "UTF-8",
**kwargs,
):
"""
Write the contained data to an HDF5 file using HDFStore.

Expand Down Expand Up @@ -2451,40 +2463,43 @@ def to_hdf(self, path_or_buf, key, **kwargs):
- 'a': append, an existing file is opened for reading and
writing, and if the file does not exist it is created.
- 'r+': similar to 'a', but the file must already exist.
format : {'fixed', 'table'}, default 'fixed'
complevel : {0-9}, optional
Specifies a compression level for data.
A value of 0 disables compression.
complib : {'zlib', 'lzo', 'bzip2', 'blosc'}, default 'zlib'
Specifies the compression library to be used.
As of v0.20.2 these additional compressors for Blosc are supported
(default if no compressor specified: 'blosc:blosclz'):
{'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy',
'blosc:zlib', 'blosc:zstd'}.
Specifying a compression library which is not available issues
a ValueError.
append : bool, default False
For Table formats, append the input data to the existing.
format : {'fixed', 'table', None}, default 'fixed'
Possible values:

- 'fixed': Fixed format. Fast writing/reading. Not-appendable,
nor searchable.
- 'table': Table format. Write as a PyTables Table structure
which may perform worse but allow more flexible operations
like searching / selecting subsets of the data.
append : bool, default False
For Table formats, append the input data to the existing.
- If None, pd.get_option('io.hdf.default_format') is checked,
followed by fallback to "fixed"
errors : str, default 'strict'
Specifies how encoding and decoding errors are to be handled.
See the errors argument for :func:`open` for a full list
of options.
encoding : str, default "UTF-8"
data_columns : list of columns or True, optional
List of columns to create as indexed data columns for on-disk
queries, or True to use all columns. By default only the axes
of the object are indexed. See :ref:`io.hdf5-query-data-columns`.
Applicable only to format='table'.
complevel : {0-9}, optional
Specifies a compression level for data.
A value of 0 disables compression.
complib : {'zlib', 'lzo', 'bzip2', 'blosc'}, default 'zlib'
Specifies the compression library to be used.
As of v0.20.2 these additional compressors for Blosc are supported
(default if no compressor specified: 'blosc:blosclz'):
{'blosc:blosclz', 'blosc:lz4', 'blosc:lz4hc', 'blosc:snappy',
'blosc:zlib', 'blosc:zstd'}.
Specifying a compression library which is not available issues
a ValueError.
fletcher32 : bool, default False
If applying compression use the fletcher32 checksum.
dropna : bool, default False
If true, ALL nan rows will not be written to store.
errors : str, default 'strict'
Specifies how encoding and decoding errors are to be handled.
See the errors argument for :func:`open` for a full list
of options.

See Also
--------
Expand Down Expand Up @@ -2526,7 +2541,19 @@ def to_hdf(self, path_or_buf, key, **kwargs):
"""
from pandas.io import pytables

pytables.to_hdf(path_or_buf, key, self, **kwargs)
pytables.to_hdf(
path_or_buf,
key,
self,
mode=mode,
complevel=complevel,
complib=complib,
append=append,
format=format,
errors=errors,
encoding=encoding,
**kwargs,
)

def to_msgpack(self, path_or_buf=None, encoding="utf-8", **kwargs):
"""
Expand Down
26 changes: 17 additions & 9 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

if TYPE_CHECKING:
from tables import File, Node # noqa:F401
from pandas.core.generic import NDFrame # noqa:F401


# versioning attribute
Expand Down Expand Up @@ -241,20 +242,27 @@ def _tables():

def to_hdf(
path_or_buf,
key,
value,
mode=None,
key: str,
value: "NDFrame",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does FrameOrSeries not work here? Don’t think will make a difference for typing but from a user / API perspective I think more intuitive than NDFrame

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

mode: str = "a",
complevel: Optional[int] = None,
complib=None,
append=None,
complib: Optional[str] = None,
append: bool = False,
format: Optional[str] = None,
errors: str = "strict",
encoding: str = "UTF-8",
**kwargs,
):
""" store this object, close it if we opened it """

if append:
f = lambda store: store.append(key, value, **kwargs)
f = lambda store: store.append(
key, value, format=format, errors=errors, encoding=encoding, **kwargs
)
else:
f = lambda store: store.put(key, value, **kwargs)
f = lambda store: store.put(
key, value, format=format, errors=errors, encoding=encoding, **kwargs
)

path_or_buf = _stringify_path(path_or_buf)
if isinstance(path_or_buf, str):
Expand Down Expand Up @@ -1032,7 +1040,7 @@ def append(
format=None,
append=True,
columns=None,
dropna=None,
dropna: Optional[bool] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -1060,7 +1068,7 @@ def append(
chunksize : size to chunk the writing
expectedrows : expected TOTAL row size of this table
encoding : default None, provide an encoding for strings
dropna : bool, default False
dropna : bool, default False
Do not write an ALL nan row to the store settable
by the option 'io.hdf.dropna_table'.

Expand Down