1

I try to figure out how to fix my problem with python. I am using pandas to create a data frame to save my data from a query. I want to extend this data frame with the column "TimeDiff" as you can see in the below table. The calculation of subtracting the time from the first element of time is working. My problem is how to create a new column with this calculated data with correct values.

Time                   Temperature     Humidity     Pulse  
2013-12-12 12:12:12    25              33           65     
2013-12-12 12:12:15    26              30           67     
2013-12-12 12:12:18    27              29           68     
2013-12-12 12:12:22    28              32           64     


Data frame should look like this:

Time                   Temperature     Humidity     Pulse     TimeDiff
2013-12-12 12:12:12    25              33           65        0
2013-12-12 12:12:15    26              30           67        3
2013-12-12 12:12:18    27              29           68        6
2013-12-12 12:12:22    28              32           64        10

My code for calculating the time difference and add it to the data frame looks like this:

df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: 'Time', 1: 'Temperature', 2: 'Humidity', 3: 'Pulse', 4:'MicValue'}, inplace=True); 
df = df.sort(['Time'], ascending=[1]);
sLength = len(df['Time'])

for i in range(sLength):
        diff = df['Time'][df.index[i]] - df['Time'][df.index[0]]
        totalSeconds = diff.total_seconds()
        # df = pd.DataFrame({'DateDiff': totalSeconds}, index=df.index)
        # df['Diff'] = df['Diff'].map(totalSeconds)

Executing this code will get the following result:

TimeDiff
740
740
740
740

So the code does not write the actual data into the data frame. It only writes the last element of the calculation of time in the whole column.

1 Answer 1

1

You can simply subtract the first index from the entire index.

df['TimeDiff'] = df.index - df.index[0]

And if you want to get seconds you can do

(df.index - df.index[0]).seconds

Or if you have your column use the dt accessor

df.TimeDiff.dt.seconds
Sign up to request clarification or add additional context in comments.

3 Comments

Oh yeah, that's much more straightforward.
Okay thank you very much, by trying your code snipped i got an error message, which looks like this: TypeError: Input must be iterable!
Make sure column Time is the index and is a datetime object. `df.index = pd.to_datetime(df.Time)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.