From 007cd273c8156ba5bbea86d29fdcb518d5deaf9a Mon Sep 17 00:00:00 2001 From: Mary Dufie Afrane Date: Wed, 9 Aug 2023 22:28:38 +0000 Subject: [PATCH 1/2] Add support for StringDtype --- python/perspective/perspective/core/data/pd.py | 6 ++++++ .../perspective/tests/table/test_table_pandas.py | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/python/perspective/perspective/core/data/pd.py b/python/perspective/perspective/core/data/pd.py index eed6a70268..f7b93b6221 100644 --- a/python/perspective/perspective/core/data/pd.py +++ b/python/perspective/perspective/core/data/pd.py @@ -78,6 +78,12 @@ def deconstruct_pandas(data, kwargs=None): if isinstance(v, pd.CategoricalDtype): data[k] = data[k].astype(str) + # convert StringDtype to str + if isinstance(data, pd.DataFrame) and hasattr(pd, "CategoricalDtype"): + for k, v in data.dtypes.items(): + if isinstance(v, pd.StringDtype): + data[k] = data[k].astype(str) + if isinstance(data, pd.DataFrame) and isinstance(data.columns, pd.MultiIndex) and isinstance(data.index, pd.MultiIndex): # Row and col pivots kwargs["group_by"].extend([str(c) for c in data.index.names]) diff --git a/python/perspective/perspective/tests/table/test_table_pandas.py b/python/perspective/perspective/tests/table/test_table_pandas.py index 085d8681d9..ce8050e5ee 100644 --- a/python/perspective/perspective/tests/table/test_table_pandas.py +++ b/python/perspective/perspective/tests/table/test_table_pandas.py @@ -597,3 +597,15 @@ def test_splitbys(self): df_both = pd.DataFrame(np.random.randn(3, 16), index=["A", "B", "C"], columns=index) table = Table(df_both) assert table.size() == 48 + + def test_table_dataframe_for_dtype_equals_string(self): + df = pd.DataFrame({"a": ["aa", "bbb"], "b": ["dddd", "dd"]}, dtype="string") + table = Table(df) + view = table.view() + + assert table.size() == 2 + + assert table.schema() == {"index": int, "a": str, "b": str} + + view_df = view.to_df() + assert view_df.to_dict() == {"index": {0: 0, 1: 1}, "a": {0: "aa", 1: "bbb"}, "b": {0: "dddd", 1: "dd"}} From dbbb9dfea847fbf537a7e288542042d05c9a63ef Mon Sep 17 00:00:00 2001 From: MaDufie Date: Mon, 14 Aug 2023 09:06:34 +0000 Subject: [PATCH 2/2] updated hasattr to check StringDtype --- python/perspective/perspective/core/data/pd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/perspective/perspective/core/data/pd.py b/python/perspective/perspective/core/data/pd.py index f7b93b6221..7ef21a2a28 100644 --- a/python/perspective/perspective/core/data/pd.py +++ b/python/perspective/perspective/core/data/pd.py @@ -79,7 +79,7 @@ def deconstruct_pandas(data, kwargs=None): data[k] = data[k].astype(str) # convert StringDtype to str - if isinstance(data, pd.DataFrame) and hasattr(pd, "CategoricalDtype"): + if isinstance(data, pd.DataFrame) and hasattr(pd, "StringDtype"): for k, v in data.dtypes.items(): if isinstance(v, pd.StringDtype): data[k] = data[k].astype(str)