From 80089c01d01fb96a08c34d45778d3dda852da2ee Mon Sep 17 00:00:00 2001 From: NumesSanguis Date: Tue, 3 Dec 2019 11:52:52 +0900 Subject: [PATCH] Added awkward.frompandas() for intuitive DataFrame -> Tables conversion --- awkward/__init__.py | 4 ++-- awkward/util.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/awkward/__init__.py b/awkward/__init__.py index 79bb8c70..466c0729 100644 --- a/awkward/__init__.py +++ b/awkward/__init__.py @@ -26,11 +26,11 @@ def concatenate(arrays, axis=0): from awkward.persist import serialize, deserialize, save, load, hdf5 from awkward.arrow import toarrow, fromarrow, toparquet, fromparquet -from awkward.util import topandas +from awkward.util import frompandas, topandas # convenient access to the version number from awkward.version import __version__ -__all__ = ["numpy", "AwkwardArray", "ChunkedArray", "AppendableArray", "IndexedArray", "SparseArray", "JaggedArray", "MaskedArray", "BitMaskedArray", "IndexedMaskedArray", "Methods", "ObjectArray", "Table", "UnionArray", "VirtualArray", "StringArray", "fromiter", "serialize", "deserialize", "save", "load", "hdf5", "toarrow", "fromarrow", "toparquet", "fromparquet", "topandas", "__version__"] +__all__ = ["numpy", "AwkwardArray", "ChunkedArray", "AppendableArray", "IndexedArray", "SparseArray", "JaggedArray", "MaskedArray", "BitMaskedArray", "IndexedMaskedArray", "Methods", "ObjectArray", "Table", "UnionArray", "VirtualArray", "StringArray", "fromiter", "serialize", "deserialize", "save", "load", "hdf5", "toarrow", "fromarrow", "toparquet", "fromparquet", "frompandas", "topandas", "__version__"] __path__ = __import__("pkgutil").extend_path(__path__, __name__) diff --git a/awkward/util.py b/awkward/util.py index bd0d373e..af174b11 100644 --- a/awkward/util.py +++ b/awkward/util.py @@ -208,7 +208,20 @@ class NDArrayOperatorsMixin(object): __abs__ = _unary_method(um.absolute, 'abs') __invert__ = _unary_method(um.invert, 'invert') -################################################################ conversion of arrays to Pandas +################################################################ conversion of arrays from/to Pandas + + +def frompandas(dataframe): + """Converts a Pandas DataFrame to an Awkward Table + + :param dataframe: Pandas DataFrame + :return: awkward.Table + """ + + import awkward.array.table + + return awkward.Table({name: dataframe[name].values for name in dataframe.columns}) + def topandas(array, flatten=False): import pandas