Skip to content
Advertisement

Resampling timestamps in a CSV

I have a CSV file that stores data from different smartphone sensors. The timestamps are elapsed nanoseconds since the program to record the data was started. Short example:

JavaScript

The time steps between the timestamps are not equal, but I would like them to be. My question is how to achieve this? I was thinking about simply downsampling the nanoseconds to microseconds using the code below. This is my first attempt that does not return an error during execution, but it returns a CSV file without the timestamps and every row after the first is completely empty.

JavaScript

I would be thankful for ways to improve my code as well as other ideas to achieve my goal in general.

Advertisement

Answer

When you resample over milliseconds, there aren’t enough values to fill consecutive buckets, so you end up with NaN’s.

If you want your timesteps to be equal while also having all buckets filled, you can find the maximum difference and use that as the resampling rate:

First, set the index to be Timedelta‘s, since it’s the time elapsed since the app started.

JavaScript

Next, resampling:

JavaScript

Output:

JavaScript

And to verify that your index is evenly spaced, it has freq='2899170N':

JavaScript

Or check via diff:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement