Skip to content

Make kwargs explicit in put, append #29957

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

Merged
merged 9 commits into from
Dec 4, 2019
6 changes: 6 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,8 @@ def to_hdf(
complib: Optional[str] = None,
append: bool_t = False,
format: Optional[str] = None,
min_itemsize=None,
Copy link
Member

Choose a reason for hiding this comment

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

could these keywords added here and in #29939 for the public API be keyword only, xref #27544

Copy link
Member Author

Choose a reason for hiding this comment

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

i like this idea, will do in a dedicated PR

Copy link
Contributor

Choose a reason for hiding this comment

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

this is Union[Optional[int], Dict[str, int]]

data_columns=None,
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional[List[str]]

errors: str = "strict",
encoding: str = "UTF-8",
**kwargs,
Expand Down Expand Up @@ -2471,6 +2473,8 @@ def to_hdf(
See the errors argument for :func:`open` for a full list
of options.
encoding : str, default "UTF-8"
min_itemsize : dict, optional
Copy link
Contributor

Choose a reason for hiding this comment

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

see above

Map column names to minimum string sizes for columns.
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
Expand Down Expand Up @@ -2530,6 +2534,8 @@ def to_hdf(
complib=complib,
append=append,
format=format,
min_itemsize=min_itemsize,
data_columns=data_columns,
errors=errors,
encoding=encoding,
**kwargs,
Expand Down
95 changes: 83 additions & 12 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def to_hdf(
complib: Optional[str] = None,
append: bool = False,
format: Optional[str] = None,
min_itemsize=None,
data_columns=None,
errors: str = "strict",
encoding: str = "UTF-8",
**kwargs,
Expand All @@ -267,11 +269,25 @@ def to_hdf(

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

Choose a reason for hiding this comment

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

yeah ok with removing the kwargs

)

path_or_buf = _stringify_path(path_or_buf)
Expand Down Expand Up @@ -957,7 +973,22 @@ def func(_start, _stop, _where):

return it.get_result(coordinates=True)

def put(self, key: str, value, format=None, append=False, **kwargs):
def put(
self,
key: str,
value: FrameOrSeries,
format=None,
index=True,
append=False,
complib=None,
complevel: Optional[int] = None,
min_itemsize=None,
nan_rep=None,
data_columns=None,
encoding=None,
errors: str = "strict",
**kwargs,
):
"""
Store object in HDFStore.

Expand Down Expand Up @@ -986,8 +1017,22 @@ def put(self, key: str, value, format=None, append=False, **kwargs):
"""
if format is None:
format = get_option("io.hdf.default_format") or "fixed"
kwargs = self._validate_format(format, kwargs)
self._write_to_group(key, value, append=append, **kwargs)
format = self._validate_format(format)
self._write_to_group(
key,
value,
format=format,
index=index,
append=append,
complib=complib,
complevel=complevel,
min_itemsize=min_itemsize,
nan_rep=nan_rep,
data_columns=data_columns,
encoding=encoding,
errors=errors,
**kwargs,
)

def remove(self, key: str, where=None, start=None, stop=None):
"""
Expand Down Expand Up @@ -1046,11 +1091,21 @@ def remove(self, key: str, where=None, start=None, stop=None):
def append(
self,
key: str,
value,
value: FrameOrSeries,
format=None,
axes=None,
index=True,
append=True,
complib=None,
complevel: Optional[int] = None,
columns=None,
min_itemsize=None,
chunksize=None,
expectedrows=None,
dropna: Optional[bool] = None,
data_columns=None,
encoding=None,
errors: str = "strict",
**kwargs,
):
"""
Expand Down Expand Up @@ -1096,8 +1151,25 @@ def append(
dropna = get_option("io.hdf.dropna_table")
if format is None:
format = get_option("io.hdf.default_format") or "table"
kwargs = self._validate_format(format, kwargs)
self._write_to_group(key, value, append=append, dropna=dropna, **kwargs)
format = self._validate_format(format)
self._write_to_group(
key,
value,
format=format,
axes=axes,
index=index,
append=append,
complib=complib,
complevel=complevel,
min_itemsize=min_itemsize,
chunksize=chunksize,
expectedrows=expectedrows,
dropna=dropna,
data_columns=data_columns,
encoding=encoding,
errors=errors,
**kwargs,
)

def append_to_multiple(
self,
Expand Down Expand Up @@ -1418,17 +1490,16 @@ def _check_if_open(self):
if not self.is_open:
raise ClosedFileError(f"{self._path} file is not open!")

def _validate_format(self, format: str, kwargs: Dict[str, Any]) -> Dict[str, Any]:
def _validate_format(self, format: str) -> str:
""" validate / deprecate formats; return the new kwargs """
kwargs = kwargs.copy()

# validate
try:
kwargs["format"] = _FORMAT_MAP[format.lower()]
format = _FORMAT_MAP[format.lower()]
except KeyError:
raise TypeError(f"invalid HDFStore format specified [{format}]")

return kwargs
return format
Copy link
Member

Choose a reason for hiding this comment

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

could just return in try block and remove intermediate variable?


def _create_storer(
self,
Expand Down