Sreeshma
Sreeshma

Reputation: 23

converting datetime.datetime object to datetime64[ns] in python for an netcdf file read with xarray module

I have an netcdf file,I read it with xarray module.I want to make a plot from it. But the problem is the datatype of my time is datetime.datetime object and I am having problem with plotting. I want to change the datatype to datetime64. I tried using pandas pd.to_datetime and also using astype('datetime'). But I am having errors. I am displaying my errors below. Plz help me. ds['time'] = pd.to_datetime(ds['time']) error:<class 'cftime._cftime.DatetimeNoLeap'> is not convertible to datetime

ds['time'] = ds['time'].astype('datetime64') error:Cannot cast datetime.datetime object from metadata [us] to according to the rule 'same_kind'

Upvotes: 0

Views: 1236

Answers (1)

spencerkclark
spencerkclark

Reputation: 2097

The error message indicates that the dates are not datetime.datetime objects; rather, they are cftime.DatetimeNoLeap objects, indicating that your data is indexed using times from a non-standard calendar. Plotting times from a non-standard calendar can be enabled by installing the nc-time-axis library.

If you'd still like to convert these objects to standard datetimes, that is possible -- see this answer -- but note that it will mean the dates used will no longer come from the same calendar as the underlying data.

Upvotes: 2

Related Questions