Skip to content

Commit

Permalink
Compat, docs and dump chmod support for data frame formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Oct 15, 2024
1 parent 8ac97e0 commit 596b1e9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/contrib/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The following example shows how a package (e.g. the :py:mod:`~law.docker` packag
matplotlib
mercurial
numpy
pandas
profiling
pyarrow
rich
Expand Down
13 changes: 13 additions & 0 deletions docs/contrib/pandas.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pandas
======

.. automodule:: law.pandas

.. contents::


Class ``DataFrameFormatter``
----------------------------

.. autoclass:: DataFrameFormatter
:members:
2 changes: 1 addition & 1 deletion law/cli/completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ _law_complete() {
# complete the "location" subcommand
elif [ "${sub_cmd}" = "location" ]; then
local words="help"
local contribs="arc awkward cms coffea docker dropbox gfal git glite hdf5 htcondor ipython keras lsf matplotlib mercurial numpy profiling pyarrow rich root singularity slack slurm tasks telegram tensorflow wlcg"
local contribs="arc awkward cms coffea docker dropbox gfal git glite hdf5 htcondor ipython keras lsf matplotlib mercurial numpy pandas profiling pyarrow rich root singularity slack slurm tasks telegram tensorflow wlcg"
local inp="${cur##-}"
inp="${inp##-}"
COMPREPLY=( $( compgen -W "$( echo ${words} )" -P "--" -- "${inp}" ) $( compgen -W "$( echo ${contribs} )" -- "${inp}" ) )
Expand Down
46 changes: 28 additions & 18 deletions law/contrib/pandas/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
__all__ = ["DataFrameFormatter"]


import pathlib
import os

from law.target.formatter import Formatter
from law.target.file import get_path
from law.logger import get_logger
from law.util import no_value


logger = get_logger(__name__)
Expand All @@ -29,6 +31,7 @@ def accepts(cls, path, mode):
@classmethod
def load(cls, path, *args, **kwargs):
path = get_path(path)

if path.endswith(".csv"):
import pandas # fmt: skip
return pandas.read_csv(path, *args, **kwargs)
Expand All @@ -49,31 +52,38 @@ def load(cls, path, *args, **kwargs):
import pandas # fmt: skip
return pandas.read_pickle(path, *args, **kwargs)

suffix = pathlib.Path(path).suffix
suffix = os.path.splitext(path)[1]
raise NotImplementedError(
f'Suffix "{suffix}" not implemented in DataFrameFormatter',
"suffix \"{}\" not implemented in DataFrameFormatter".format(suffix),
)

@classmethod
def dump(cls, path, obj, *args, **kwargs):
path = get_path(path)
_path = get_path(path)
perm = kwargs.pop("perm", no_value)

if path.endswith(".csv"):
return obj.to_csv(path, *args, **kwargs)
if _path.endswith(".csv"):
ret = obj.to_csv(_path, *args, **kwargs)

if path.endswith(".json"):
return obj.to_json(path, *args, **kwargs)
elif _path.endswith(".json"):
ret = obj.to_json(_path, *args, **kwargs)

if path.endswith(".parquet"):
return obj.to_parquet(path, *args, **kwargs)
elif _path.endswith(".parquet"):
ret = obj.to_parquet(_path, *args, **kwargs)

if path.endswith((".h5", ".hdf5")):
return obj.to_hdf(path, *args, **kwargs)
elif _path.endswith((".h5", ".hdf5")):
ret = obj.to_hdf(_path, *args, **kwargs)

if path.endswith((".pickle", ".pkl")):
return obj.to_pickle(path, *args, **kwargs)
elif _path.endswith((".pickle", ".pkl")):
ret = obj.to_pickle(_path, *args, **kwargs)

suffix = pathlib.Path(path).suffix
raise NotImplementedError(
f'Suffix "{suffix}" not implemented in DataFrameFormatter',
)
else:
suffix = os.path.splitext(path)[1]
raise NotImplementedError(
"suffix \"{}\" not implemented in DataFrameFormatter".format(suffix),
)

if perm != no_value:
cls.chmod(path, perm)

return ret

0 comments on commit 596b1e9

Please sign in to comment.