Home > other >  Convert dataframe to xarray with datetime coordinate
Convert dataframe to xarray with datetime coordinate

Time:09-21

I have the following pandas dataframe:

enter image description here

I would like to convert it to an xarray dataset, but when I use the code below, it uses the date column as a variable.

dsn = nino.to_xarray()
print(dsn)

enter image description here

I need the date column to be the time coordinate (ie datetime64). How do I do this?

Further to this, I tried the following:

dsn = dsn.assign_coords(time=dsn['DATE']) 
dsn = dsn.drop('YR') dsn = dsn.drop('MON') 
dsn = dsn.drop('DATE') 

enter image description here

But when I try select certain years (using dsn.sel(time=slice()) I get an error: 'no index found for coordinate time'. Any guidance would be greatly appreciated?

CodePudding user response:

To convert a variable into datetime format, see the following: xarray: coords conversion to datetime64

Xarray distinguishes between coordinates and dimensions. If you want to be able to easily slice your data by a variable, you'll need to set it as a dimension of your dataset.

See here for more info on xarray's data structures: https://docs.xarray.dev/en/stable/user-guide/data-structures.html

Presuming you'd like to get rid of the index and replace it with the time coordinate, the following should work:

dsn = dsn.set_coords('DATE').swap_dims({'index': 'DATE'})
# Set 'date' as a coordinate, and then swap the index with the 'date' column.
  • Related