Skip to content

Commit 518e105

Browse files
committed
datatree to dataframe dealing with wierd attributes and speeding up process
1 parent 940c242 commit 518e105

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/valenspy/_utilities/_datatree.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,26 @@ def datatree_to_dataframe(dt: xr.DataTree, add_attributes=False):
110110
df["id"] = str(key)
111111

112112
if add_attributes:
113+
attr_dict = {}
113114
if isinstance(add_attributes, bool): #If add_attributes is True, we add all attributes. If it's a list, we only add the specified attributes.
114-
for attr in ds.attrs:
115-
df[attr] = ds.attrs[attr]
116-
for var in ds.data_vars: #Add all the variable related attributes
117-
for attr in ds[var].attrs:
118-
df[f"{var}_{attr}"] = ds[var].attrs[attr]
115+
attr_dict.update(ds.attrs) #The dataset attributes
116+
for var in ds.data_vars: #The variable attributes
117+
attr_dict.update({f"{var}_{attr}": ds[var].attrs[attr] for attr in ds[var].attrs})
119118
else:
120119
for attr in add_attributes:
121120
#if attr is a substring of any attribute in ds.attrs:
122121
for ds_attr in ds.attrs:
123122
if attr in ds_attr:
124-
df[attr] = ds.attrs[ds_attr]
123+
attr_dict[attr] = ds.attrs[ds_attr]
125124
break #Break the loop after finding the first match to avoid adding multiple attributes with the same substring.
125+
#If the attribute is not a string try to cast it to a string, else drop it
126+
for attr in attr_dict:
127+
if not isinstance(attr_dict[attr], str):
128+
try:
129+
attr_dict[attr] = str(attr_dict[attr])
130+
except:
131+
del attr_dict[attr]
132+
df = df.assign(**attr_dict)
126133
data_frames.append(df)
127134

128135
return pd.concat(data_frames, axis=0).reset_index()

0 commit comments

Comments
 (0)