I have a pandas data frame, df_data, want to use the pandas index.asof() method to find the nearest row to a specified time. The times I have are in seconds (type = float64) (see below).
Originally, the index was made to be a DateTimeIndex:
In [12]: df_data.index = pd.to_datetime(df_data.index, coerce=True)
df_data.index.dtype
Out[12]: dtype('<M8[ns]')
Then, I changed the index to be in seconds from the initial time:
In [22]: ## Convert the index from DateTimeIndex to a float64
## that is the number of seconds from the initial measurement
df_data.index = (df_data.index - df_data.index[0])/np.timedelta64(1,'s')
In [23]: df_data.index.dtype
Out[23]: dtype('float64')
But when I try to use the asof method with a float, I get a TypeError:
In [24]: df_data.index.asof(10.0)
...
TypeError: Cannot compare type 'Timestamp' with type 'float'
I have tried to use datetime, datetime.fromtimestamp, etc., but haven't been able to resolve the issue.
asofonly works with a DatetimeIndex, and not with other index types.asofbeing limited to DateTimeIndex. I have edited my code to use the method (and save the results) before changing my index from DateTime to float. I have edited the question above.