Skip to content

Commit

Permalink
Merge pull request #2319 from MaDufie/finos/add-1237-update
Browse files Browse the repository at this point in the history
Add support for StringDtype (fixes #1237)
  • Loading branch information
texodus authored Nov 9, 2023
2 parents 785b1ee + dbbb9df commit 7f122b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/perspective/perspective/core/data/pd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "StringDtype"):
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])
Expand Down
12 changes: 12 additions & 0 deletions python/perspective/perspective/tests/table/test_table_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}

0 comments on commit 7f122b7

Please sign in to comment.