1

I have a data frame in which I have the distance and speed out of which I calculated the time. Now I want to add a column in which I can give its a fixed DateTime value for the first row and it will automatically add the time and make it to the next value to the dateTime column

Currently the dataframe looks like:

x_coordinate    y_coordinate    z_coordinate    speed   Distance    Time1
-22                -2.28           -0.1         300     1           0.2
-21                -2.28           -0.1         300     1           0.2
-20                -2.28           -0.1         300     1           0.2
-19                -2.28           -0.1         300     1           0.2
-18                -2.28           -0.1         300     1           0.2
-17                -2.28           -0.1         300     1           0.2
-16                -2.28           -0.1         300     1           0.2
-15                -2.28           -0.1         300     1           0.2
-14                -2.28           -0.1         300     1           0.2
-13.2674           -2.601          -0.1         300 0.7998398339667759  0.15996796679335518
-13.039            -3.5743         -0.1         300 0.9997396911196436  0.1999479382239287
-12.7392           -4.5281         -0.1         300 0.9998072214182092  0.19996144428364185
-12.3697           -5.4571         -0.1         300 0.9997856020167519  0.1999571204033504

The time is in seconds like the 0.2 seconds that I calculated with speed 300 mm/sec and distance = 1 mm. Now I want to add a column named as DateTime which has the first Dattime value hardcoded and the consecutive value calulated by the time columns like

    Datetime
    2019-02-21 03:50:39.000 --> this is hardcoded
    2019-02-21 03:50:39.200 --> this to be calculated by adding 0.2 seconds from row 1.
     and so on

1 Answer 1

1

Try:

df['new_time'] = (pd.to_datetime('2019-02-21 03:50:39.000') + 
                  pd.to_timedelta(df.Time1.shift().cumsum(), unit='s')
                 )

Output (df['new_time']):

0    2019-02-21 03:50:39.000000000
1    2019-02-21 03:50:39.200000000
2    2019-02-21 03:50:39.400000000
3    2019-02-21 03:50:39.600000000
4    2019-02-21 03:50:39.800000000
5    2019-02-21 03:50:40.000000000
6    2019-02-21 03:50:40.200000000
7    2019-02-21 03:50:40.400000000
8    2019-02-21 03:50:40.600000000
9    2019-02-21 03:50:40.800000000
10   2019-02-21 03:50:40.959967967
11   2019-02-21 03:50:41.159915905
12   2019-02-21 03:50:41.359877349
Name: new_time, dtype: datetime64[ns]
Sign up to request clarification or add additional context in comments.

Comments

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.