1

I have a data with accelrometer data which i wanted to visualise. But when i convert time from object to timedelta using panda it shows me different graph. what can i do to plot the data the right.

import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_json("Right_191018_10-51-15_Eating_Floor_FrenchToast_ForkKnifeHand.json")
df[['Date','Time','GMT']] = df['loggingTime'].str.split(' ',expand=True)
dfright=pd.DataFrame({
    'X':df['accelerometerAccelerationX'],
    'Y':df['accelerometerAccelerationY'],
    'Z':df['accelerometerAccelerationZ'], 
    'Time':df['Time']
          })
print(dfright.dtypes)
plt.figure(figsize=(20,10))
plt.figtext('.5','.99','Acelrometer Data',fontsize=18,ha='center')

x,=plt.plot(
    dfright['Time'],
             dfright['X'],
    label='x'
    )
plt.legend([x],['x'])
plt.xlabel('time ')
plt.ylabel('accelro')
plt.title('Right Hand')
plt.gca().xaxis.set_major_locator(plt.MaxNLocator(10))

dfright['Time']=pd.to_timedelta(dfright['Time'])
print(dfright.dtypes)
plt.figure(figsize=(20,10))
plt.figtext('.5','.99','Acelrometer Data',fontsize=18,ha='center')

x,=plt.plot(
    dfright['Time'],
             dfright['X'],
    label='x'
    )
 plt.legend([x],['x'])
 plt.xlabel('time ')
 plt.ylabel('accelro')
 plt.title('Right Hand')
 plt.gca().xaxis.set_major_locator(plt.MaxNLocator(10))

data

            X         Y         Z          Time
0    0.187256 -0.113373 -0.978668  10:51:15.627
1    0.203720 -0.121597 -0.967041  10:51:15.645
2    0.210968 -0.117950 -0.956497  10:51:15.648
3    0.221909 -0.114548 -0.949478  10:51:15.651
4    0.231415 -0.108597 -0.939728  10:51:15.656
..        ...       ...       ...           ...
992  0.275085  0.186905 -0.960556  10:58:28.910
993  0.251862  0.170105 -0.967285  10:58:28.925
994  0.266571  0.177551 -0.969528  10:58:28.940
995  0.277298  0.194107 -0.974319  10:58:28.955
996  0.273453  0.204010 -0.980560  10:58:28.973

here is the dtype before converting to timedelta

X       float64 
Y       float64 
Z       float64 
Time     object 
dtype: object

enter image description here

here is the dtype after converting to timedelta

            X         Y         Z            Time
0    0.187256 -0.113373 -0.978668 10:51:15.627000
1    0.203720 -0.121597 -0.967041 10:51:15.645000
2    0.210968 -0.117950 -0.956497 10:51:15.648000
3    0.221909 -0.114548 -0.949478 10:51:15.651000
4    0.231415 -0.108597 -0.939728 10:51:15.656000
..        ...       ...       ...             ...
992  0.275085  0.186905 -0.960556 10:58:28.910000
993  0.251862  0.170105 -0.967285 10:58:28.925000
994  0.266571  0.177551 -0.969528 10:58:28.940000
995  0.277298  0.194107 -0.974319 10:58:28.955000
996  0.273453  0.204010 -0.980560 10:58:28.973000
X               float64
Y               float64
Z               float64
Time    timedelta64[ns]
dtype: object

enter image description here

Muhammad Ahmed
  • 131
  • 1
  • 1
  • 7

2 Answers2

0

From this topic converting a time string to seconds in python

timestr = '00:04:23'

ftr = [3600,60,1]

sum([a*b for a,b in zip(ftr, map(int,timestr.split(':')))])
Output is 263Sec.

You can run function to convert string to s (need some modifications in your code)

Later, subtract each value from the first one and it's done.

pedro_bb7
  • 1,601
  • 3
  • 12
  • 28
0

I'm not sure why you'd want to use the dtype Timedelta? It denotes a difference in time.

For your (plotting) purposes I believe you are looking for .to_datetime().

Try replacing

dfright['Time']=pd.to_timedelta(dfright['Time'])

with

dfright['Time']=pd.to_datetime(dfright['Time'])
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
gosuto
  • 5,422
  • 6
  • 36
  • 57
  • actually i have two accelrometer data for both hands, performing some activity. i wanted to know what graph it will be shown at the same time. thats why i used timedelta. i have also tried your solution as well but graph is same as timedelta – Muhammad Ahmed Nov 22 '19 at 15:16