Skip to content

Instantly share code, notes, and snippets.

@ranaroussi
Last active December 14, 2016 07:30
Show Gist options
  • Save ranaroussi/07964dc2e3b49d2f44bfca798333367d to your computer and use it in GitHub Desktop.
Save ranaroussi/07964dc2e3b49d2f44bfca798333367d to your computer and use it in GitHub Desktop.
A Generic method to save Pandas objects to file, based on file extension found in the output path.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A Generic method to save Pandas objects to file,
based on file extension found in the output path.
Usage:
df.to_file("/path/to/file.csv")
"""
def df_to_file(data, output=None, **kwargs):
if output is None:
return
if ".csv" in output:
return data.to_csv(output, **kwargs)
elif ".h5" in output:
return data.to_hdf(output, 0, **kwargs)
elif (".pickle" in output) | (".pkl" in output):
return data.to_pickle(output, **kwargs)
return
from pandas.core.base import PandasObject
PandasObject.to_file = df_to_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment