3

How do I remove Date, Hours and Seconds from a pandas datetime, so that I'm left with only the minutes? I have a table of dates in the format:

Date
2015-04-18 23:33:58
2015-04-19 14:32:08
2015-04-20 18:42:44
2015-04-20 21:41:19

and I want:

Date
33
32
42
41

The code I'm trying to use is:

fiveMin['Date'] = fiveMin['Date'] - pd.Timedelta(fiveMin['Date'], unit='s')

to remove the seconds, however I'm getting the error:

Value must be Timedelta, string, integer, float, timedelta or convertible
Josh Kidd
  • 816
  • 2
  • 14
  • 35

1 Answers1

6

If the dtype is already datetime you can use dt accessor to return just the minute attribute:

In [43]:
df['Date'].dt.minute

Out[43]:
0    33
1    32
2    42
3    41
Name: Date, dtype: int64

If needed convert using to_datetime:

df['Date'] = pd.to_datetime(df['Date'])`

If the dates are strings and are well formed you could just split on : and extract the second to last split:

In [46]:
df['Date'].str.split(':').str[-2]

Out[46]:
0    33
1    32
2    42
3    41
Name: Date, dtype: object

This returns a string series however, if you want ints then you can cast to int using astype(int):

In [47]:
(df['Date'].str.split(':').str[-2]).astype(int)

Out[47]:
0    33
1    32
2    42
3    41
Name: Date, dtype: int32
EdChum
  • 376,765
  • 198
  • 813
  • 562