12

I have a dataframe that looks like this

df

[output]:
date        time
2020-02-28  00:30:45
2020-02-28  00:30:45
2020-03-09  00:21:06
2020-03-09  00:21:06
2020-03-09  00:21:06

with

df.time.dtype

[output]: dtype('<m8[ns]')

I want to extract the minutes in the time variable with the following command

df.time.dt.minute

but instead, I have this error

AttributeError: 'TimedeltaProperties' object has no attribute 'minute'

Does someone know how to fix this problem?

3 Answers 3

17

your column 'time' is of dtype timedelta as the error tells you; you could use the total_seconds() method to convert to seconds and divide by 60 to get the minutes.

If you want a full-featured datetime column, combine 'date' and 'time'. Then you can use .dt.minute.

Ex:

import pandas as pd
df = pd.DataFrame({'time': pd.to_timedelta(['00:30:45','00:30:45','00:21:06','00:21:06','00:21:06']),
                   'date': pd.to_datetime(['2020-02-28','2020-02-28','2020-03-09','2020-03-09','2020-03-09'])})

# to get the "total minutes":
df['minutes'] = df['time'].dt.total_seconds()/60
df['minutes']
# 0    30.75
# 1    30.75
# 2    21.10
# 3    21.10
# 4    21.10
# Name: minutes, dtype: float64

[pd.Timedelta docs]

# to get a column of dtype datetime:
df['DateTime'] = df['date'] + df['time']

# now you can do:
df['DateTime'].dt.minute
# 0    30
# 1    30
# 2    21
# 3    21
# 4    21
# Name: DateTime, dtype: int64
0
3

If you have not converted to a datetime dataframe do that first then you create a new column like this

df['minute'] = df['date'].dt.minute

or this method here

  df[new]= df[column].map(lambda x: datetime.datetime(x.minutes))
1
  • 1
    Thanks for your answer. I just tried and now it gives me the "AttributeError: 'Timedelta' object has no attribute 'minutes'" error. I don't understand the difference between 'Timedelta' and 'TimedeltaProperties'.
    – Le Noff
    Jun 9, 2020 at 11:40
3

@Fobersteiner's answer is very good, but just for completeness, I would like to add that you could also divide your column of dtype timedelta by a fixed timedelta. For instance:

from datetime import timedelta
import pandas as pd
df = pd.DataFrame({'time': pd.to_timedelta(['00:30:45','00:30:45','00:21:06','00:21:06','00:21:06']),
                   'date': pd.to_datetime(['2020-02-28','2020-02-28','2020-03-09','2020-03-09','2020-03-09'])})

# to get the "total minutes":
df['minutes'] = df['time'] / timedelta(minutes=1)  # <--
df['minutes']

Out[9]: 
0    30.75
1    30.75
2    21.10
3    21.10
4    21.10
Name: minutes, dtype: float64

Though personally, I prefer @Fobersteiner's method.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.