如何在Python中为现有类添加方法?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Python中为现有类添加方法?相关的知识,希望对你有一定的参考价值。
我有这个非常方便的高级pd.DataFrame
保存功能,我想添加到pandas
。如何将此方法添加到pd.DataFrame
类?
def to_file(df, path, sep=" ", 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: {path}",
f"sep: {repr(sep)}",
f"compression: {compression}",
f"pickled: {pickled}",
sep="
",
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)
答案
使用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=" ", compression="infer", pickled="infer", verbose=False, **args):
...
以上是关于如何在Python中为现有类添加方法?的主要内容,如果未能解决你的问题,请参考以下文章