Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

This is my df it contains timestamps but not indexed:

   timestamp

0  2020-10-23 12:20:00-04:00  

1  2020-10-23 12:30:00-04:00  

2  2020-10-23 12:40:00-04:00

3  2020-10-23 12:50:00-04:00

I am trying to create the new column minutes that contain a total number of minutes as counted from 00:00:00.

So the above would output

   timestamp                      minutes

0  2020-10-23 12:20:00-04:00      740

1  2020-10-23 12:30:00-04:00      750

2  2020-10-23 12:40:00-04:00      760

3  2020-10-23 12:50:00-04:00      770

I tried the pd.timedelta however I'm unable to set the begin time to reference from. Importantly, minutes should be an integer and not a string. Any suggestions will be appreciated.

1 Answer

0 votes
by (36.8k points)

You can use the dt.normalize to get your day, subtract, and divide the timdelta 1T:

df['minutes'] = (df.timestamp - df.timestamp.dt.normalize()) // pd.Timedelta('1T')

Another option is(obviously easy):

df['minutes'] = df.timestamp.dt.hour * 60 + df.timestamp.dt.minute

Output:

                  timestamp  minutes

0 2020-10-23 12:20:00-04:00      740

1 2020-10-23 12:30:00-04:00      750

2 2020-10-23 12:40:00-04:00      760

3 2020-10-23 12:50:00-04:00      770

If you are a beginner and want to know more about Data Science the do check out the Data Science course 

Browse Categories

...