Chianti5
Chianti5

Reputation: 243

Python datetime transform

I want to convert a column in my data frame with value like '12:51 AM' to '01' (which equals to a 24 hour clock time of '01:00'). And yes, I want to round it to the nearest hour.

For example, say my DataFrame is:

Time
12:51 AM
1:51 AM
2:03 AM
4:23 PM

I want to change it to:

Time
01
02
02
16

My method:

First, split each string, say '12:51 AM', into two parts - '12:51' and 'AM'. Then, use loop and if to achieve this.

I'm looking for more efficient ways, like using datetime package.

Thanks a lot.

Upvotes: 1

Views: 339

Answers (3)

JohnE
JohnE

Reputation: 30424

There's nothing wrong with the existing answers, but they aren't using any of the built in pandas solutions which can make things easier. If you're starting with things in string format you can convert to datetime and then use pandas dt to extract hours and such.

df['Time2'] = pd.to_datetime(df.Time)

       Time               Time2
0  12:51 AM 2015-04-20 00:51:00
1   1:51 AM 2015-04-20 01:51:00
2   2:03 AM 2015-04-20 02:03:00
3   4:23 PM 2015-04-20 16:23:00

Now you can extract hours/minutes/etc. very easily with dt methods.

df['Time2'].dt.hour + df['Time2'].dt.minute.div(30).astype(int)

0     1
1     2
2     2
3    16

Useful docs:

dt accessor

converting to timestamps

Upvotes: 1

dparadis28
dparadis28

Reputation: 101

I agree with cdonts. Here is a pythonic solution

from time import strptime as st

times = "12:51 AM", "1:51 AM", "2:03 AM", "4:23 PM"
for time in times:
    print(st(time, '%I:%M %p').tm_hour+st(time, '%I:%M %p').tm_min//30)

Depending on your needs you may want to store the results in a list (or datatype of your choosing)

times = [st(time, '%I:%M %p').tm_hour+st(time, '%I:%M %p').tm_min//30 for time in("12:51 AM", "1:51 AM", "2:03 AM", "4:23 PM")]

It is not clear where you will be pulling your times from. If they are not user inputs and are stored somewhere for later retrieval you may want to write a function that retrieves the values and return a list which can then be used like so

times = [st(time, '%I:%M %p').tm_hour+st(time, '%I:%M %p').tm_min//30 for time in retrieve(args)]

Hope this helps

Upvotes: 0

cdonts
cdonts

Reputation: 9599

time.strptime() fits your needs.

from time import strptime

def transform(data_frame):
    time = strptime(data_frame, "%I:%M %p")
    hour = time.tm_hour
    if time.tm_min >= 30:
        if hour < 23:
            hour += 1
        else:
            hour = 0
    return hour

values = "12:51 AM", "1:51 AM", "2:03 AM", "4:23 PM"

for value in values:
    print(transform(value))

Output:

1
2
2
16

Hope it helps!

Upvotes: 1

Related Questions