Home > Back-end >  I have a data sample at 700HZ but I cannot create timestamps exactly at 700HZ
I have a data sample at 700HZ but I cannot create timestamps exactly at 700HZ

Time:07-03

I have sensor data sampled at 700HZ but they don't have timestamps and I want to generate timestamps using pandas.data_range() but I couldn't get freq exactly at 700HZ

Currently I create timestamps like this

time_stamps_at_700 = pd.date_range(datetime(2022, 1, 1, hour=00, minute=00), periods=len(labels), freq='1.430615ms')

Since 700HZ is not exactly divisable I couldn't equalize the frequency to 700hz

Is there a way I can generate timestamps exactly at 700HZ?

CodePudding user response:

Rather than using a range object, which depends on the the accumulation of the step value, you could use np.linspace to give you a set number of intervals between two exact endpoints.

The following creates a linspace with 700 intervals from 0 to 1e9 nanoseconds (1 second), and you can see the first and last elements land on the second mark exactly:

In [3]: pd.to_timedelta(np.linspace(0, 1e9, 700).astype('timedelta64[ns]'))
Out[3]:
TimedeltaIndex([          '0 days 00:00:00', '0 days 00:00:00.001430615',
                '0 days 00:00:00.002861230', '0 days 00:00:00.004291845',
                '0 days 00:00:00.005722460', '0 days 00:00:00.007153075',
                '0 days 00:00:00.008583690', '0 days 00:00:00.010014306',
                '0 days 00:00:00.011444921', '0 days 00:00:00.012875536',
                ...
                '0 days 00:00:00.987124463', '0 days 00:00:00.988555078',
                '0 days 00:00:00.989985693', '0 days 00:00:00.991416309',
                '0 days 00:00:00.992846924', '0 days 00:00:00.994277539',
                '0 days 00:00:00.995708154', '0 days 00:00:00.997138769',
                '0 days 00:00:00.998569384',           '0 days 00:00:01'],
               dtype='timedelta64[ns]', length=700, freq=None)
  • Related