【问题标题】:Extracting the hour from a datetime64[ns] variable从 datetime64[ns] 变量中提取小时
【发布时间】:2016-11-26 19:03:17
【问题描述】:

我有一列已使用 pandas 转换为日期时间,因此现在采用 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
  '''

【问题讨论】:

    标签: python-2.7 pandas


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 这修复了它!谢谢!
    猜你喜欢
    • 2019-04-29
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 2021-02-19
    相关资源
    最近更新 更多