Last active
December 14, 2016 07:30
-
-
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.
This file contains hidden or 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
#!/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