Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting the hour from a datetime64[ns] variable

I have a column that I've converted to datetime using pandas so it's now in the format datetime64.

LOCAL_TIME_ON 
2014-06-21 15:32:09
2014-06-07 20:17:13

I want to extract the hour to a new column. The only thing I've found that works is below, however, I get a SettingWithCopyWarning. Does anyone have a cleaner way I can do this?

TRIP_INFO_TIME['TIME_HOUR'] = pd.DatetimeIndex(TRIP_INFO_TIME['LOCAL_TIME_ON']).hour.astype(int)
C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.1.4\helpers\pydev\pydevconsole.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  '''
like image 691
Amber Z. Avatar asked Jul 22 '16 19:07

Amber Z.


1 Answers

try this:

TRIP_INFO_TIME['TIME_HOUR'] = TRIP_INFO_TIME['LOCAL_TIME_ON'].dt.hour
like image 139
MaxU - stop WAR against UA Avatar answered Oct 19 '22 23:10

MaxU - stop WAR against UA