Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
切片日期时间函数:Python_Python_Pandas_Function_Date_Time - Fatal编程技术网

切片日期时间函数:Python

切片日期时间函数:Python,python,pandas,function,date,time,Python,Pandas,Function,Date,Time,我有一个带有以下列的日期框: 0 2019-06-30 17:31:43 1 2019-06-30 07:07:41 2 2019-06-30 17:11:46 3 2019-06-30 12:13:50 4 2019-06-30 12:13:55 5 2019-06-30 03:01:53 6 2019-06-30 07:22:02 7 2019-06-30 18:12:47 8 2019-06-30 21:38:19 9 2019-

我有一个带有以下列的日期框:

0    2019-06-30 17:31:43
1    2019-06-30 07:07:41
2    2019-06-30 17:11:46
3    2019-06-30 12:13:50
4    2019-06-30 12:13:55
5    2019-06-30 03:01:53
6    2019-06-30 07:22:02
7    2019-06-30 18:12:47
8    2019-06-30 21:38:19
9    2019-06-30 03:01:58
10   2019-06-30 10:06:16
11   2019-06-30 03:46:43
12   2019-06-30 00:44:24
13   2019-06-30 00:44:29
14   2019-06-30 00:44:32
15   2019-06-30 00:44:33
16   2019-06-30 19:32:09
17   2019-06-30 09:09:42
18   2019-06-30 11:52:10
19   2019-06-30 18:30:00
20   2019-06-30 01:26:56
21   2019-06-30 19:29:02
22   2019-06-30 11:54:39
23   2019-06-30 16:54:47
24   2019-06-30 06:00:49
25   2019-06-30 18:51:17
26   2019-06-30 05:54:55
27   2019-06-30 19:22:43
28   2019-06-29 23:06:12
29   2019-06-30 04:26:07
我想要的是一个新的专栏,它有一天的时间[早上,下午,晚上]

下一行返回一个布尔值

test['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00') 
此行返回一个过滤后的数据帧,并将值更改为morning[这是我想要的]

test[test['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00')] = 'Morning'
这是可行的,但现在该列混合了
d类型
,这使得很难转换剩余的时间戳,因为一些时间戳现在是
str
,而另一些时间戳是
datetime64[ns]

我尝试了以下方法,但没有成功:

def time_of_day(df, date_col):
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00')] = 'Morning'
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('12:00:01','18:00:00')] = 'Afternoon'
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('18:00:01','23:59:59')] = 'Evening'
  return df
def time_of_day(df):
  if df['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00'):
    return 'Morning'
  elif df['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('12:00:01','18:00:00'):
    return 'Afternoon'
  else:
    return 'Evening'

test.apply(time_of_day, axis=1)
这将完美地执行第一行,但以下行的命运与上面的相同[混合数据类型]

我也试过这个,但运气不好:

def time_of_day(df, date_col):
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00')] = 'Morning'
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('12:00:01','18:00:00')] = 'Afternoon'
  df[date_col] = df[df[date_col].dt.strftime('%H:%M:%S').between('18:00:01','23:59:59')] = 'Evening'
  return df
def time_of_day(df):
  if df['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('00:00:00','12:00:00'):
    return 'Morning'
  elif df['visitStartTime_aest'].dt.strftime('%H:%M:%S').between('12:00:01','18:00:00'):
    return 'Afternoon'
  else:
    return 'Evening'

test.apply(time_of_day, axis=1)
知道我遗漏了什么吗?或者关于如何执行的任何指导


谢谢

让我们使用
pd.cut

pd.cut(s.dt.hour,[-0.001,12,18,24],labels=['Morning','Afternoon','Evening'])

这起作用了——谢谢你。如果可能,是否有办法使上述功能正常工作?没有回答的义务-只是好奇看看它会是什么样子[更多供我自己学习]@AdrianC between将直接进行筛选而不是返回bool,因此我建议
df['visitStartTime\u aest'].dt.strftime('%H:%M:%S')>'00:00'&df['visitStartTime\u aest'].dt.strftime('%H:%M:%S'))