Reputation: 3502
Is it possible with pandas to transform a column Date
that looks like this: (I think ISO format without the T
)
2020-06-29 14:04:21.000000
2020-06-29 14:19:26.000000
2020-06-29 14:34:30.000000
2020-06-29 14:49:35.000000
2020-06-29 15:04:39.000000
2020-06-29 15:19:44.000000
2020-06-29 15:34:48.000000
2020-06-29 15:49:53.000000
2020-06-29 16:20:02.000000
2020-06-29 16:35:07.000000
2020-06-29 16:50:11.000000
2020-06-29 17:05:16.000000
2020-06-29 17:20:20.000000
2020-06-29 17:35:25.000000
2020-06-29 17:50:30.000000
2020-06-29 18:05:34.000000
2020-06-29 18:20:39.000000
2020-06-29 18:35:43.000000
2020-06-29 18:50:48.000000
2020-06-29 19:05:52.000000
To a format like this with only hours & minutes. Rounding to closest minute would be good enough. Can I go right from time.time.now()
to a format that looks this below? Thanks for any tips...
1/19/2019 0:30
1/19/2019 0:45
1/19/2019 1:00
1/19/2019 1:15
1/19/2019 1:30
1/19/2019 1:45
1/19/2019 2:00
1/19/2019 2:15
1/19/2019 2:30
1/19/2019 2:45
1/19/2019 3:00
1/19/2019 3:15
1/19/2019 3:30
1/19/2019 3:45
Upvotes: 0
Views: 53
Reputation: 353
def formatTime(origTD):
date = origTD.split(' ')[0]
time = origTD.split(' ')[1]
day = int(date.split('-')[2])
month = int(date.split('-')[1])
year = date.split('-')[0][-2:]
hour = time.split(':')[0]
minute = time.split(':')[1]
newTD = str(month)+'/'+str(day)+'/'+year+' '+hour+':'+minute
return newTD
Upvotes: 0
Reputation: 5730
import pandas as pd
data = {'Date': ['2019-05-30 00:00:44.000000', '2019-05-30 00:00:50.000000','2019-05-30 00:23:26.000000']}
df = pd.DataFrame(data=data)
df['New_Date'] = pd.to_datetime(df['Date']).dt.round('min').dt.strftime('%m/%d/%Y %H:%M')
Output:
Date New_Date
0 2019-05-30 00:00:44 05/30/2019 00:01
1 2019-05-30 00:00:50 05/30/2019 00:01
2 2019-05-30 00:23:26 05/30/2019 00:23
Upvotes: 2
Reputation: 3999
If you are looking to go from the time.time()
method to the desired format you can use this:
time.strftime('%m/%d/%Y %H:%M')
Furthermore if your pandas variable is a valid Datetime object you can apply strftime
to it.
Upvotes: 1