Removing leap day from leap years in xarray dataset


I have Netcdf file loaded in an xarray dataset and I want to make daily climatologies without the leap day that is, without 29th Feb included in it. I’m trying the Dataset.drop method by the syntax is not so intuitive for me. Here is the Dataset

Dimensions:        (lat: 1, lev: 1, lon: 720, time: 27133)
* lon            (lon) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
* lev            (lev) float32 1.0
* time           (time) datetime64[ns] 2000-01-02T18:00:00 ... 2018-07-30
Dimensions without coordinates: lat
Data variables:
Var1              (time, lev, lon) float32 ...
Var2              (time, lat, lon) float64 ...
Var3              (time, lat, lon) float64 ...

I tried

ds_N_R.drop(['Var1', 'Var2', 'Var3'], time='2000-02-29')
>>TypeError: drop() got an unexpected keyword argument 'time'
##another approach
ds_N_R.sel(time='2000-02-29').drop(['Var1', 'Var2', 'Var3'])
## gives not the result I intended
Dimensions:  (lev: 1, lon: 720, time: 4)
* lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 ... 178.5 179.0 179.5
* lev      (lev) float32 1.0
* time     (time) datetime64[ns] 2000-02-29 ... 2000-02-29T18:00:00
Data variables:

How do I proceed here? It would be great to know if there is a direct method through which I can calculate daily climatologies considering only 365 days of a year but I would also like to know how to remove data from a particular time step when required.

Asked By: Light_B



The right way to use drop() here would be:
ds_N_R.drop([np.datetime64('2000-02-29')], dim='time')

But I think this could actually be more cleanly done with an indexing operation, e.g.,
ds_N_R.sel(time=~((ds_N_R.time.dt.month == 2) & ( == 29)))

Answered By: shoyer

You can convert your calendar to a non_leap one using xarray’s convert_calendar. That is ds_N_R.convert_calendar('noleap').

Per the xarray documentation ( "If the source and target calendars are either no_leap, all_leap or a standard type, only the type of the time array is modified. When converting to a leap year from a non-leap year, the 29th of February is removed from the array."

Answered By: user39360
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.