Hasanah
Hasanah

Reputation: 15

how to convert format time "hhmmss.ss" to "hh:mm:ss.ss" in python

I have time in series, let's say 002959.20. I would like to change in this format 00:25:59.20 with timezone (+7.00) or 07:25:59. I have tried with 'strftime', but it's not working. How can i change the format? really appreciate any help! :)

edit Here's my data:

$PU    003114.00
$PU    003114.20
$PU    003114.40
$PU    003114.60
$PU    003114.80
Name: Time, dtype: object

Here's my code:

y =  (New[Time])
import time
time.strftime(y,'%H:%M:%S.%f')

output:
TypeError: strftime() argument 1 must be str, not Series

and I tried to convert to string

TypeError: Tuple or struct_time argument required

Upvotes: 0

Views: 2187

Answers (2)

Andrew L
Andrew L

Reputation: 7038

You can convert to datetime this way:

Using your data:

s
0    003114.00
1    003114.20
2    003114.80
Name: test, dtype: object

# reassign the Series to datetime format
s = pd.to_datetime(s, format='%H%M%S.%f')

s
0   1900-01-01 00:31:14.000
1   1900-01-01 00:31:14.200
2   1900-01-01 00:31:14.800
Name: test, dtype: datetime64[ns]

Adding 7 hours:

s = s + pd.offsets.Hour(7)
0   1900-01-01 07:31:14.000
1   1900-01-01 07:31:14.200
2   1900-01-01 07:31:14.800
Name: test, dtype: datetime64[ns]

Upvotes: 1

Lucy The Brazen
Lucy The Brazen

Reputation: 137

There most certanly is a better solution (this is literally the worst possible solution), but you could:

Add 70000 to your current time

Convert it to a string

and group them like this: string[0] string[1] : string[2] string[3] : string[4-8]

Upvotes: 0

Related Questions