1
$\begingroup$

I have data that looks like:

 1495573445.162, 0, 0.021973, 0.012283, -0.995468, 1
 1495573445.172, 0, 0.021072, 0.013779, -0.994308, 1
 1495573445.182, 0, 0.020157, 0.015717, -0.995575, 1
 1495573445.192, 0, 0.017883, 0.012756, -0.993927, 1
 1495573445.202, 0, 0.021194, 0.012161, -0.994705, 1
 1495573445.212, 0, 0.019638, 0.013718, -0.994019, 1
 1495573445.222, 0, 0.019440, 0.010803, -0.994476, 1
 1495573445.232, 0, 0.018112, 0.010849, -0.993073, 1
 1495573445.242, 0, 0.020157, 0.011154, -0.994644, 1
 1495573445.252, 0, 0.020340, 0.010040, -0.995804, 1
 1495573445.262, 0, 0.017792, 0.009857, -0.996078, 1
 1495573445.272, 0, 0.020538, 0.010239, -0.994858, 1

This is accelerometer data where the data frame columns are labeled "Time stamp", "Time skipped", "x", "y", "z", and "label" with the index set to "Time stamp".

The sampling rate is around 100Hz. How should I create a sliding window in this case?

I came up with this:

def sliding_window(data, window_size, step_size):
    data = pd.rolling_window(data, window_size)
    data = data[step_size - 1 :: step_size]
    print data
    return data

I doubt this is the correct answer, and I don't know what to set window_size and step_size given that I have a 100Hz sampling rate.

$\endgroup$
2
$\begingroup$

You can use the built-in Pandas functions to do it:

df["Time stamp"] = pd.to_datetime(df["Time stamp"]) # Convert column type to be datetime
indexed_df = df.set_index(["Time stamp"])           # Create a datetime index
indexed_df.rolling(100)                             # Create rolling windows
indexed_df.rolling(100).mean()                      # Then apply functions to rolling windows

This code is not 100% correct because the datetime conversion is not correct but it should help you get started.

$\endgroup$
  • $\begingroup$ data is passed in from a function that does df = pd.read_csv(filename, header=None, names=['timestamp', 'time skipped', 'x', 'y', 'z', 'label']).set_index('timestamp') followed by df.assign(dx=df.x.diff(), dy=df.y.diff(), dz=df.z.diff()) $\endgroup$ – dirtysocks45 Jul 18 '17 at 18:21
  • $\begingroup$ With that said, do I need the first two lines? $\endgroup$ – dirtysocks45 Jul 18 '17 at 18:22
  • $\begingroup$ "timestamp" column needs to be cast as datetime type to then later leverage rolling method. Pandas might automagically do that for you. I would be explicit about datetime casting. It is tricky. $\endgroup$ – Brian Spiering Jul 18 '17 at 18:34
  • $\begingroup$ When I try to do df['timestamp'] = pd.to_datetime(df['timestamp']) after df = pd.read_csv(filename, header=None, names=['timestamp', 'time skipped', 'x', 'y', 'z', 'label']).set_index('timestamp') I get KeyError: 'timestamp' $\endgroup$ – dirtysocks45 Jul 18 '17 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.