本文整理汇总了Python中feather.write_dataframe方法的典型用法代码示例。如果您正苦于以下问题:Python feather.write_dataframe方法的具体用法?Python feather.write_dataframe怎么用?Python feather.write_dataframe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类feather
的用法示例。
在下文中一共展示了feather.write_dataframe方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: health_xml_to_feather
# 需要导入模块: import feather [as 别名]
# 或者: from feather import write_dataframe [as 别名]
def health_xml_to_feather(zip_file, output_file, remove_zip=False):
with tempfile.TemporaryDirectory() as tmpdirname:
f = zipfile.ZipFile(zip_file, "r")
f.extractall(tmpdirname)
xml_path = os.path.join(tmpdirname, "apple_health_export/export.xml")
tree = etree.parse(xml_path)
records = tree.xpath("//Record")
df = pd.DataFrame([{key: r.get(key) for key in ALL_KEYS}
for r in records])
# Clean up key types
for k in DATETIME_KEYS:
df[k] = pd.to_datetime(df[k])
for k in NUMERIC_KEYS:
# some rows have non-numeric values, so coerce and drop NaNs
df[k] = pd.to_numeric(df[k], errors="coerce")
df = df[df["value"].notnull()]
feather.write_dataframe(df, output_file)
if remove_zip:
os.remove(zip_file)
示例2: save
# 需要导入模块: import feather [as 别名]
# 或者: from feather import write_dataframe [as 别名]
def save(data, filename):
"""Pickles the passed data (with the highest available protocol) to disk using the passed filename.
If the filename ends in '.gz' then the data will additionally be GZIPed before saving.
If filename ends with '.feather' or '.fthr', mlcrate will try to save the file using feather (for dataframes).
Note that feather does not support compression.
Keyword arguments:
data -- The python object to pickle to disk (use a dict or list to save multiple objects)
filename -- String with the relative filename to save the data to. By convention should end in '.pkl' or 'pkl.gz' or '.feather'
"""
folders = os.path.dirname(filename)
if folders:
os.makedirs(folders, exist_ok=True)
fl = filename.lower()
if fl.endswith('.gz'):
if fl.endswith('.feather.gz') or fl.endswith('.fthr.gz'):
# Since feather doesn't support writing to the file handle, we can't easily point it to gzip.
raise NotImplementedError('Saving to compressed .feather not currently supported.')
else:
fp = gzip.open(filename, 'wb')
pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)
else:
if fl.endswith('.feather') or fl.endswith('.fthr'):
if str(type(data)) != "<class 'pandas.core.frame.DataFrame'>":
raise TypeError('.feather format can only be used to save pandas DataFrames')
import feather
feather.write_dataframe(data, filename)
else:
fp = open(filename, 'wb')
pickle.dump(data, fp, protocol=pickle.HIGHEST_PROTOCOL)
示例3: _dump_table
# 需要导入模块: import feather [as 别名]
# 或者: from feather import write_dataframe [as 别名]
def _dump_table(self, dst, fmt='feather', components=None, *args, **kwargs):
""" Save batch data to table formats
Args:
dst: str - a path to dump into
fmt: str - format: feather, hdf5, csv
components: str or tuple - one or several component names
"""
filename = dst
components = tuple(components or self.components)
data_dict = {}
for comp in components:
comp_data = self.get(component=comp)
if isinstance(comp_data, pd.DataFrame):
data_dict.update(comp_data.to_dict('series'))
elif isinstance(comp_data, np.ndarray):
if comp_data.ndim > 1:
columns = [comp + str(i) for i in range(comp_data.shape[1])]
comp_dict = zip(columns, (comp_data[:, i] for i in range(comp_data.shape[1])))
data_dict.update({comp: comp_dict})
else:
data_dict.update({comp: comp_data})
else:
data_dict.update({comp: comp_data})
_data = pd.DataFrame(data_dict)
if fmt == 'feather':
feather.write_dataframe(_data, filename, *args, **kwargs)
elif fmt == 'hdf5':
_data.to_hdf(filename, *args, **kwargs) # pylint:disable=no-member
elif fmt == 'csv':
_data.to_csv(filename, *args, **kwargs) # pylint:disable=no-member
else:
raise ValueError('Unknown format %s' % fmt)
return self
示例4: save_df
# 需要导入模块: import feather [as 别名]
# 或者: from feather import write_dataframe [as 别名]
def save_df(df, path, index=False):
if path == '-' or path is None:
print(default_csv_writer(df, None, index=index))
elif file_format(path) != 'feather':
default_csv_writer(df, path, index=index)
elif featherpmm and feather:
featherpmm.write_dataframe(featherpmm.Dataset(df, name='verification'),
path)
elif feather:
feather.write_dataframe(df, path)
else:
raise Exception('The Python feather module is not installed.\n'
'Use:\n pip install feather-format\n'
'to add capability.\n')
示例5: to_feather
# 需要导入模块: import feather [as 别名]
# 或者: from feather import write_dataframe [as 别名]
def to_feather(df, path):
"""
Write a DataFrame to the feather-format
Parameters
----------
df : DataFrame
path : string file path, or file-like object
"""
path = _stringify_path(path)
if not isinstance(df, DataFrame):
raise ValueError("feather only support IO with DataFrames")
feather = _try_import()
valid_types = {'string', 'unicode'}
# validate index
# --------------
# validate that we have only a default index
# raise on anything else as we don't serialize the index
if not isinstance(df.index, Int64Index):
raise ValueError("feather does not support serializing {} "
"for the index; you can .reset_index()"
"to make the index into column(s)".format(
type(df.index)))
if not df.index.equals(RangeIndex.from_range(range(len(df)))):
raise ValueError("feather does not support serializing a "
"non-default index for the index; you "
"can .reset_index() to make the index "
"into column(s)")
if df.index.name is not None:
raise ValueError("feather does not serialize index meta-data on a "
"default index")
# validate columns
# ----------------
# must have value column names (strings only)
if df.columns.inferred_type not in valid_types:
raise ValueError("feather must have string column names")
feather.write_dataframe(df, path)