From f7bda8c3cfe4d14d6c49d5b490ab84342920f823 Mon Sep 17 00:00:00 2001 From: Dhivya Kanakabala Date: Wed, 27 Nov 2019 17:31:50 +0530 Subject: [PATCH] Issue_1909_Add_a_to_dict_method_on_SFrame --- src/python/turicreate/data_structures/sframe.py | 11 +++++++++++ src/python/turicreate/test/test_sframe.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/src/python/turicreate/data_structures/sframe.py b/src/python/turicreate/data_structures/sframe.py index cb5db0c05f..23a31f42f4 100644 --- a/src/python/turicreate/data_structures/sframe.py +++ b/src/python/turicreate/data_structures/sframe.py @@ -5881,6 +5881,17 @@ def _group(self, key_columns): """ gsf = GroupedSFrame(self, key_columns) return gsf + + def to_dict(self): + dict_sframe = {} + for key in self.column_names(): + sa = SArray(self[key]) + val_list = [] + for val in sa: + val_list.append(val) + dict_sframe.update({key:val_list}) + del val_list + return dict_sframe @property def shape(self): diff --git a/src/python/turicreate/test/test_sframe.py b/src/python/turicreate/test/test_sframe.py index 91a0c8cfcd..c2d5418e23 100644 --- a/src/python/turicreate/test/test_sframe.py +++ b/src/python/turicreate/test/test_sframe.py @@ -3787,6 +3787,12 @@ def test_export_empty_SFrame(self): sf2 = SFrame.read_json(f.name) _assert_sframe_equal(sf, sf2) + def test_to_dict(self): + sf = SFrame({'col1': [1, 2],'col2': [0.5, 0.75]}) + result = sf.to_dict() + expected = {'col1': [1, 2], 'col2': [0.5, 0.75]} + self.assertEqual(result, expected) + if __name__ == "__main__": import sys