Stream: python-questions

Topic: Xarray.assign problem


view this post on Zulip Brendan Myers (Oct 04 2023 at 20:42):

Hi everybody!

I would just love another set of eyes to look over my code... I have used xarray.Dataset.assign a good number of times in other places, but it seems to be acting weird in this instance. I am trying to add this new list, 'lapse_rate', as a variable in df (which is an existing dataset), but it seems to want to add it to the coordinates for some reason. (I have even tried to use xarray.Dataset.reset_coords to just move it despite this, but it gives me an error)

Thanks!

image.png

view this post on Zulip Hendrik Grosselindemann (Oct 04 2023 at 22:50):

You can assign it with a tuple specifiying the dimension:
df = df.assign(lapse_rate = ('height', lr))

view this post on Zulip Brendan Myers (Oct 05 2023 at 00:10):

That's great, thanks Hendrik! Some error I had gotten had suggested doing that, but I couldn't figure out the formatting, so I appreciate you showing me how it's done!


Last updated: May 16 2025 at 17:14 UTC