is0707
is0707

Reputation: 109

Need to add milliseconds to time

I have a data frame with some columns out of which one column is Time. I have calculated milliseconds from the data give to me and add that as a separate columns named as milliseconds. Now i concatenated both the columns to get a single column with milliseconds.

Problem is 1.) I need milliseconds in three decimal place so I need to round that off. 2.) When I am concatenating the time stamp with milliseconds so when seconds get increment with one the millisecond should start with zero again. Like after

In the below code 'sample' is constant = 60 and 'RDT Sequence' is value starting from one and increment by 1 for every next row.

2019-02-21 03:50:39:2
2019-02-21 03:50:40:216666666667
But instead I should get    
2019-02-21 03:50:40:00

Time    Miliseconds  \                    Time1  


0    2019-02-21 03:50:39  0166666666667   2019-02-21 03:50:39:0166666666667
1    2019-02-21 03:50:39  0333333333333   2019-02-21 03:50:39:0333333333333
2    2019-02-21 03:50:39             05   2019-02-21 03:50:39:05
3    2019-02-21 03:50:39  0666666666667   2019-02-21 03:50:39:0666666666667 
4    2019-02-21 03:50:39  0833333333333   2019-02-21 03:50:39:0833333333333 
5    2019-02-21 03:50:39              1   2019-02-21 03:50:39:1 
6    2019-02-21 03:50:39   116666666667   2019-02-21 03:50:39:116666666667
7    2019-02-21 03:50:39   133333333333   2019-02-21 03:50:39:133333333333
8    2019-02-21 03:50:39             15   2019-02-21 03:50:39:15
9    2019-02-21 03:50:39   166666666667   2019-02-21 03:50:39:166666666667
10   2019-02-21 03:50:39   183333333333   2019-02-21 03:50:39:183333333333
11   2019-02-21 03:50:39              2   2019-02-21 03:50:39:2
12   2019-02-21 03:50:40   216666666667   2019-02-21 03:50:40:216666666667
13   2019-02-21 03:50:40   233333333333   2019-02-21 03:50:40:233333333333
14   2019-02-21 03:50:40             25   2019-02-21 03:50:40:25
15   2019-02-21 03:50:40   266666666667   2019-02-21 03:50:40:266666666667

df['Miliseconds'] = df['RDT Sequence']/sample
df['Miliseconds'] = df['Miliseconds'].map(str).str[2:]
df['Time1'] = df['Time'].map(str) +":"+ df['Miliseconds'].map(str)  
print(df)

Upvotes: 0

Views: 3551

Answers (1)

Will
Will

Reputation: 1551

Assuming "Time" column is a string, you want a pandas Timestamp. Then you want milliseconds in a numeric format. Given that is in a format that represents the fractional part of a number we convert it to a fraction, round it off to 3 and then multiply by 1000 to get milliseconds. Lastly we use it to build a timedelta so it can be easily added to the Timestamp.

# The Miliseconds column is not currently in milliseconds, it is
# a fraction of a second (0.016)
# Rounding it off is easy
df["Miliseconds"] = df["Miliseconds"].round(3)
# Make it milliseconds, not fraction of a seconds
df["Miliseconds"] = df["Miliseconds"] * 1000

# Now add it to Time using a Timedelta
tdelta = pd.to_timedelta(df["Miliseconds"], unit="ms")
df["Time1"] = df["Time"] + tdelta

Many of the routines above can be done on a single line. I expanded here to illustrate each operation.

Upvotes: 1

Related Questions