I have a workflow using the xarray package where I am grouping an xarray object using groupby() and then applying a function that turns it to a dataframe and performs some calculations using the map() function.
I would like to rename a column in the output dataframe using the name of the group similar to how you would in this example : pandas apply example
Though it appears the xarray groups do not have a 'name' attribute that I can use plug-and-play that examples code. I also tried using the group.labels but that did not work either.
General workflow for xr.groupby.map()
# Dummy Dataset
ds = xr.Dataset(
{"foo": (("x", "y"), np.random.rand(4, 3))},
coords={"x": [10, 20, 30, 40], "letters": ("x", list("abba"))}
)
# Dataset as array
arr = ds["foo"]
# User defined function
def standardize(x):
return (x - x.mean()) / x.std()
# Apply the function to each group
arr.groupby('letters').map(standardize)
Functionality I'm shooting for (does not work as written)
def to_df_and_rename(grouped_array):
df_out = grouped_array.to_dataframe()
df_out = df_out[['desired_col']]
df_out = df_out.rename(columns = {"desired_col" : grouped_array.name})
return(df_out)
Thanks.