从datetime64 [ns]变量中提取小时

时间:2016-07-22 19:15:51

标签: python-2.7 pandas

我有一个专栏,我已经使用pandas将其转换为datetime,现在它的格式为datetime64。

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

我想将小时提取到新列。我发现作品的唯一内容就在下面,但是,我得到了一个SettingWithCopyWarning。有没有人能以更清洁的方式做到这一点?

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
  '''

1 个答案:

答案 0 :(得分:7)

试试这个:

TRIP_INFO_TIME['TIME_HOUR'] = TRIP_INFO_TIME['LOCAL_TIME_ON'].dt.hour