Home > OS >  I have a column named 'time' in which some rows have 10:00 as only 10 and other rows as on
I have a column named 'time' in which some rows have 10:00 as only 10 and other rows as on

Time:12-31

I have a dataset in which there is a column named "Time" of the type object. There are some rows given as 10 for 10:00 and others as 1000. how do I convert this column to time format.

weather['Time'] = pd.to_datetime(weather['Time'], format='%H:%M').dt.Time

this is the code I used. I am getting this error, ValueError: time data '10' does not match format '%H:%M' (match)

CodePudding user response:

You can convert column to required time format first like this

weather= pd.DataFrame(['1000','10:00','10','1000'],columns=list("Time"))
def convert_time(x):
    if len(x) == 2:
        return f'{x}:00'
    if ':' not in x:
        return x[:2]   ':'   x[2:]
    return x
wheather.Time= wheather.Time.apply(convert_time)
wheather.Time
Out[1]: 
0    10:00
1    10:00
2    10:00
3    10:00

To convert it to datetime

wheather.Time = pd.to_datetime(wheather.Time)

Just the time component

wheather.Time.dt.time
Out[92]: 
0    10:00:00
1    10:00:00
2    10:00:00
3    10:00:00

CodePudding user response:

Another possible solution, which is based on the following ideas:

  1. Replace :, when there is one, by the empty string.

  2. Right pad with zeros, so that all entries will have 4 digits.

  3. Use pd.to_datetime to convert to the wanted time format.

weather = pd.DataFrame({'Time': ['20', '1000', '12:30', '0930']})
    
pd.to_datetime(weather['Time'].str.replace(':', '').str.pad(
    4, side='right', fillchar='0'), format='%H%M').dt.time

Output:

0    20:00:00
1    10:00:00
2    12:30:00
3    09:30:00
Name: Time, dtype: object
  • Related