如何在Python中为现有类添加方法?

时间:2018-03-06 20:08:51

标签: python pandas class object methods

我有这个非常方便的高级pd.DataFrame保存功能,我想添加到pandas。如何将此方法添加到pd.DataFrame类?

def to_file(df, path, sep="\t", compression="infer", pickled="infer", verbose=False, **args):
    _ , ext = os.path.splitext(path)
    # Serialization
    if pickled == "infer":
        if ext in {".pkl", ".pgz", ".pbz2"}:
            pickled = True
        else:
            pickled = False
    # Compression
    if compression == "infer":
        if pickled:
            if ext == ".pkl":
                compression = None
            if ext == ".pgz":
                compression = "gzip"
            if ext == ".pbz2":
                compression = "bz2"
        else:
            compression = None
            if path.endswith(".gz"):
                compression = "gzip"
            if path.endswith(".bz2"):
                compression = "bz2"
    if verbose:
        print(
            f"path:\t{path}", 
            f"sep:\t{repr(sep)}",
            f"compression:\t{compression}",
            f"pickled:\t{pickled}", 
            sep="\n", 
            file=sys.stderr,
        )
    if pickled == False:
        df.to_csv(path, sep=sep, compression=compression, **args)
    if pickled == True:
        df.to_pickle(path, compression=compression, **args)

1 个答案:

答案 0 :(得分:1)

使用python类继承。这将允许您使用Pandas数据框中的所有方法,并仍然定义自己的方法。

import pandas as pd

class NewDF(pd.DataFrame)
    def __init__(self, *args):
        pd.DataFrame.__init__(self, *args)

    def to_file(df, path, sep="\t", compression="infer", pickled="infer", verbose=False, **args):
        ...