-1

i have already tried these but by this we can only get hour/minute/second but not both minute and second

In[7]: df['B'] = df['A'].dt.components['hours']

df

Out[7]:

     A  B

0 02:00:00 2

1 01:00:00 1


from this timedelta format

0 0 days 00:02:02.246000

1 0 days 00:01:59.127000

i need only minutes and seconds, how to get it as below

02:02

01:59

  • 1
    You'll have to write your own formatter; see [Format timedelta to string](https://stackoverflow.com/questions/538666/format-timedelta-to-string) – FObersteiner Jun 22 '21 at 10:21

1 Answers1

0

You can convert the timedelta to total seconds and use divmod function to extract minutes and seconds from the total seconds.

Edit: Removed lambda function from the first operation.

    df['sec'] = df['A'].dt.total_seconds().astype(int)
    df['B'] = df.apply(lambda row: str(divmod(divmod(row['sec'], 3600)[1], 60)[0]).zfill(2)+':'+str(divmod(divmod(row['sec'], 3600)[1], 60)[1]).zfill(2), axis=1)
redant96
  • 1
  • 3