nakedbird226
nakedbird226

Reputation: 77

Python Converting minute number to datetime

df['minutes'] = [0, 360, 1000, 1439]

I have a pandas df with a column called minutes that I would like to convert to datetime.

The desired result would look like the following (or a similar datetime format):

df['new_min'] = ["00:00","06:00","16:40","23:59"]

This didn't seem to work quite the way I wanted it to.

df['new_min']=pd.TimedeltaIndex(df['minutes'], unit='m')

Upvotes: 0

Views: 108

Answers (1)

If what you want is to convert minutes to an hour what you can do is this:

def toHour(minutes):
    return str(timedelta(minutes=minutes%1440))[:-3]

Now to do that for every element :

df['new_min'] = list(map(lambda x: toHour(int(x)), df['minutes']))

EDIT 1 If you prefer "01:30" instead of "1:30" you can do

def toHour(minutes):
        hour = str(timedelta(minutes=minutes%1440))[:-3]
        return "{0:0>5}".format(hour)

Upvotes: 1

Related Questions