小编EJS*_*Suh的帖子

熊猫:四舍五入到最近的一小时

我有一个带有时间戳的列

 start_time: 
 0    2016-06-04 05:18:49
 1    2016-06-04 06:50:12
 2    2016-06-04 08:16:02
 3    2016-06-04 15:05:13
 4    2016-06-04 15:24:25
Run Code Online (Sandbox Code Playgroud)

我想在start_time列上使用一个函数将分钟> = 30舍入到下一个小时.

 def extract_time(col):
      time = col.strftime('%H:%M')
      min= int(time.strip(':')[1])
      hour= int(time.strip(':')[0])
      if min >= 30:
           return hour + 1
      return hour
Run Code Online (Sandbox Code Playgroud)

然后我想用圆形小时创建一个新的列'小时':

 df['hour'] = df['start_time'].apply(extract_time)
Run Code Online (Sandbox Code Playgroud)

我没有得到带有舍入时间的"小时"专栏,而是得到以下内容:

 0    <function extract_hour at 0x128722b90>
 1    <function extract_hour at 0x128722b90>
 2    <function extract_hour at 0x128722b90>
 3    <function extract_hour at 0x128722b90>
 4    <function extract_hour at 0x128722b90>
Run Code Online (Sandbox Code Playgroud)

python datetime-format python-datetime pandas

2
推荐指数
2
解决办法
2619
查看次数

标签 统计

datetime-format ×1

pandas ×1

python ×1

python-datetime ×1