Last active
November 22, 2019 14:52
-
-
Save ny0m/2e52f1aff4ba6cb641319845c0d66983 to your computer and use it in GitHub Desktop.
A nice way to accept Pandas DataFrames as function arguments. Read more: https://brr.ai/posts/dataframe/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dataframe_requires(column_names: List[str]): | |
""" | |
Wraps a function that takes a pandas.DataFrame as its first argument. | |
Ensures that functions wrapped by this can safely assume that the required | |
columns are available. | |
Also ensures that a copy of the given dataframe is used by the function to | |
prevent modifying parent dataframes kept in memory. | |
Meant to assist with documenting functions that require a dataframe type as | |
a parameter. | |
Sample usage: | |
``` | |
@dataframe_requires(["a", "b"]) | |
def example(big_data, another_argument, optional_argument=False): | |
# `big_data` will always contain the "a" and "b" columns. | |
# Feel free to modify the dataframe. | |
``` | |
""" | |
def decorator_require(func): | |
@functools.wraps(func) | |
def wrapper_require(df, *args, **kwargs): | |
try: | |
# Indexing this way returns a copy of the dataframe. | |
df = df[list(set(column_names))] | |
except KeyError as e: | |
raise Exception( | |
f"@dataframe_requires couldn't find key in `{func.__name__}` input.\n" | |
f"Original exception: {e}" | |
) | |
return func(df, *rest, **kwargs) | |
return wrapper_require | |
return decorator_require |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment