'AttributeError: 'TimedeltaProperties' object has no attribute 'minute'
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?
Solution 1:[1]
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
# 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
Solution 2:[2]
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))
Solution 3:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Osuolale Emmanuel |
Solution 3 | Dr_Zaszu? |