10

I have this really convenient high-level pd.DataFrame saving function that I want to add to pandas. How can I add this method to the pd.DataFrame class?

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)
2
  • maybe a duplicate of: stackoverflow.com/questions/972/… Commented Mar 6, 2018 at 20:12
  • no its not he wants to add to the class not to an instance of the class Commented Dec 12, 2023 at 18:21

1 Answer 1

6

Use python class inheritance. This will allow you to use all the methods in a Pandas data frame and still define your own methods.

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):
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

pd.DataFrame.to_file = to_file also works but I like the above approach. Thanks, this will come in handy for other things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.