Skip to content

Commit cdf8f39

Browse files
committed
Correction in data_structures_utils.py concatenate_heterogeneous_DataArrays for changes with string types of pandas
1 parent f9e43c2 commit cdf8f39

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

tvb_contrib/tvb/contrib/scripts/utils/data_structures_utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ def concatenate_heterogeneous_DataArrays(data, concat_dim_name,
882882
from pandas import Series
883883
from xarray import concat
884884
from pandas import Index
885+
885886
if isinstance(data, (dict, Series)):
886887
if data_keys is None:
887888
data_keys = ensure_list(data.keys())
@@ -891,7 +892,20 @@ def concatenate_heterogeneous_DataArrays(data, concat_dim_name,
891892
if name is None:
892893
name = data.name
893894
data = ensure_list(data.values)
894-
data = concat(data, Index(data_keys, name=concat_dim_name), fill_value=fill_value)
895+
# Idiomatic xarray approach: build a dict of new coords and use assign_coords
896+
cleaned_data = []
897+
for da in data:
898+
updated_coords = {}
899+
for c_name, coord in da.coords.items():
900+
if "string" in str(coord.dtype).lower():
901+
updated_coords[c_name] = coord.values.astype(object)
902+
# assign_coords returns a new DataArray, leaving the original untouched
903+
if updated_coords:
904+
da = da.assign_coords(updated_coords)
905+
906+
cleaned_data.append(da)
907+
# Pass the newly mapped list of DataArrays to concat
908+
data = concat(cleaned_data, Index(data_keys, name=concat_dim_name), fill_value=fill_value, join='outer')
895909
data.name = name
896910
if transpose_dims:
897911
data = data.transpose(*transpose_dims)

0 commit comments

Comments
 (0)