0
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')
1
  • any attempt on your end? Commented Jan 26, 2018 at 17:18

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

7 Comments

Simpler: list(map(toHour, df['minutes']))
what data type is expected for this method. I am getting a type error. Not all arguments converted during string formatting
It should be an array of strings
hmm sorry. Looks like my column is an object.
Try using pandas apply with the function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.