I have my time stored in the format YYYYMMDDhhmm in my dataframe, eg. 200902110403.
Pandas can automatically convert this into a datetime object and I'm doing that like this:
temp_date=(pd.to_datetime(indexed_data.index.str[0:12], infer_datetime_format=True)).to_pydatetime()
(I don't fully understand the difference between a datetime object and a datetimeindex but I don't think that's the source of my problems)
I then use the data2num function from the netcdf4 library to convert this to days since my reference time like this,
days=date2num(temp_date, 'days since 2009-01-01')
This works and returns the days as I want
array([ 212.03333333, 212.03333333, 212.03472222, ..., 242.95416667,
242.95416667, 242.99583333])
The problem is that it doesn't seem to all work in one go and I don't understand why.
Why doesn't this work?
indexed_data['date']=(pd.to_datetime(indexed_data.index.str[0:12], infer_datetime_format=True)).to_pydatetime()
indexed_data['days']=date2num(indexed_data['date'], 'days since 2009-01-01')
TypeError: ufunc subtract cannot use operands with types dtype('
but this does:
temp_date=(pd.to_datetime(indexed_data.index.str[0:12],infer_datetime_format=True)).to_pydatetime()
indexed_data['date']=temp_date
indexed_data['fdays']=date2num(temp_date, 'days since 2009-01-01')
Thanks!