Skip to content

Instantly share code, notes, and snippets.

@somiandras
Created April 4, 2019 12:25
Show Gist options
  • Save somiandras/40ccefc39b68ee896687babd4d0258a6 to your computer and use it in GitHub Desktop.
Save somiandras/40ccefc39b68ee896687babd4d0258a6 to your computer and use it in GitHub Desktop.
Logging function to be used in pd.DataFrame.pipe().
def pipe_log(data, logger, message=None, level='DEBUG'):
'''
Logging function to be used in pd.DataFrame.pipe(). Insert anywhere
in a method chain to log head and shape of dataframe/series along
with a custom message with the set logging level, then returns the
dataframe unchanged.
Params:
- data: pandas.DataFrame or pandas.Series
- logger: logging.Logger
- message: str (optional, default=None)
- level: str (optional, default='DEBUG')
Example:
# Logs message, head and shape before calling sum() on dataframe
df.pipe(pipe_log, logger, message='Test', level='INFO').sum()
'''
lvl = getattr(logging, level, 10)
if message is not None:
logger.log(lvl, message)
logger.log(lvl, data.head())
logger.log(lvl, data.shape)
return data
@somiandras
Copy link
Author

Could be further abstracted by accepting list of DataFrame properties and methods that should be logged, instead of hard coded .head() and .shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment