From 00a6dd6b5178050a644bb1697af420a9062b6281 Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 5 Dec 2022 12:25:54 -0500 Subject: [PATCH 1/2] add to and from df functionality to dictarray --- merlin/systems/dag/dictarray.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/merlin/systems/dag/dictarray.py b/merlin/systems/dag/dictarray.py index 1da6989c5..3a0610abf 100644 --- a/merlin/systems/dag/dictarray.py +++ b/merlin/systems/dag/dictarray.py @@ -18,6 +18,7 @@ import numpy as np +from merlin.core.dispatch import get_lib from merlin.core.protocols import SeriesLike try: @@ -225,6 +226,25 @@ def copy(self): """ return DictArray(self._columns.copy()) + def to_df(self): + """ + Create a DataFrame from the DictArray + """ + df = get_lib.DataFrame() + for col in self.columns: + df[col] = get_lib.Series(self[col]) + return df + + @classmethod + def from_df(cls, df): + """ + Create a DictArray from a DataFrame + """ + array_dict = {} + for col in df.columns: + array_dict[col] = df[col].to_numpy() + return cls(array_dict) + def _array_lib(): """Dispatch to the appropriate library (cupy or numpy) for the current environment""" From 59e54a9d0a45b2286ee4fa4986020372dd521a8e Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 5 Dec 2022 19:18:07 -0500 Subject: [PATCH 2/2] fix for to and from df functions --- merlin/systems/dag/dictarray.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/merlin/systems/dag/dictarray.py b/merlin/systems/dag/dictarray.py index 3a0610abf..eb18e45a6 100644 --- a/merlin/systems/dag/dictarray.py +++ b/merlin/systems/dag/dictarray.py @@ -230,9 +230,9 @@ def to_df(self): """ Create a DataFrame from the DictArray """ - df = get_lib.DataFrame() + df = get_lib().DataFrame() for col in self.columns: - df[col] = get_lib.Series(self[col]) + df[col] = get_lib().Series(self[col]) return df @classmethod