Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to datetime in python

Tags:

python

pandas

I have a time data in a column and trying to figure out how can I get it in datetime format

2000
2100
2300
2355
0
1
5 
10
100
105
330

My question is how can I get these in datetime format:

output should be:

20:00:00
21:00:00
23:00:00
23:55:00
00:00:00
00:01:00
00:05:00
00:10:00
01:00:00
01:05:00
03:30:00

tried:

1. da =  pd.to_datetime(330, format='%H%M')
   output: '03:30:00'

2. d= str(datetime.timedelta(minutes = 55 ))
   output : '0:55:00'

But if I apply 1. to 100 it gives 10 hrs.

   eg: da =  pd.to_datetime(100, format='%H%M')
   output: '10:00:00'
like image 216
Novice_Coder Avatar asked Sep 22 '26 13:09

Novice_Coder


1 Answers

Try,

pd.to_datetime(df['time'].astype(str).str.zfill(4), format = '%H%M').dt.time


0     20:00:00
1     21:00:00
2     23:00:00
3     23:55:00
4     00:00:00
5     00:01:00
6     00:05:00
7     00:10:00
8     01:00:00
9     01:05:00
10    03:30:00
like image 107
Vaishali Avatar answered Sep 25 '26 04:09

Vaishali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!