Reputation: 11
I have a Dataframe column with the date and time combined as a single int, How can I convert it to a standard Date/Time format?
Date-time
0 202205220000
1 202205220005
2 202205220010
3 202205220015
4 202205220020
5 202205220025
6 202205220030
7 202205220035
8 202205220040
9 202205220045
10 202205220050
11 202205220055
12 202205220100
to
Date-time
0 2022-05-22 00:00
1 2022-05-22 00:05
2 2022-05-22 00:10
3 2022-05-22 00:15
4 2022-05-22 00:20
5 2022-05-22 00:25
6 2022-05-22 00:30
7 2022-05-22 00:35
8 2022-05-22 00:40
9 2022-05-22 00:45
10 2022-05-22 00:50
11 2022-05-22 00:55
12 2022-05-22 01:00
Upvotes: 0
Views: 60
Reputation: 260335
You can use to_datetime
with the '%Y%m%d%H%M'
format:
df['Date-time'] = pd.to_datetime(df['Date-time'], format='%Y%m%d%H%M')
output:
Date-time
0 2022-05-22 00:00:00
1 2022-05-22 00:05:00
2 2022-05-22 00:10:00
3 2022-05-22 00:15:00
4 2022-05-22 00:20:00
5 2022-05-22 00:25:00
6 2022-05-22 00:30:00
7 2022-05-22 00:35:00
8 2022-05-22 00:40:00
9 2022-05-22 00:45:00
10 2022-05-22 00:50:00
11 2022-05-22 00:55:00
12 2022-05-22 01:00:00
Upvotes: 1
Reputation: 120391
Use format
as parameter of pd.to_datetime
:
df['Date-time'] = pd.to_datetime(df['Date-time'], format='%Y%m%d%H%M')
print(df)
# Output
Date-time
0 2022-05-22 00:00:00
1 2022-05-22 00:05:00
2 2022-05-22 00:10:00
3 2022-05-22 00:15:00
4 2022-05-22 00:20:00
5 2022-05-22 00:25:00
6 2022-05-22 00:30:00
7 2022-05-22 00:35:00
8 2022-05-22 00:40:00
9 2022-05-22 00:45:00
10 2022-05-22 00:50:00
11 2022-05-22 00:55:00
12 2022-05-22 01:00:00
Upvotes: 1