Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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_String_Jupyter Notebook_Timestamp - Fatal编程技术网

Python 将字符串转换为时间戳并拆分数据

Python 将字符串转换为时间戳并拆分数据,python,string,jupyter-notebook,timestamp,Python,String,Jupyter Notebook,Timestamp,我有这样一个数据集: data=pd.DataFrame({'time':['13:30','9:20','18:12','19:00','8:20']}) 我将时间分割为仅获取小时部分,并将其添加到表中 data['hour']=data.Time.apply(lambda x:int(x.split(':')[0])) 现在我想把时间分成一天中的3部分和3次用餐时间,假设时间间隔(6,11]是早餐,(11,15]是午餐,(15,20]是晚餐 我试过这个代码,但似乎不起作用 def时间段(

我有这样一个数据集:

data=pd.DataFrame({'time':['13:30','9:20','18:12','19:00','8:20']})
我将时间分割为仅获取
小时
部分,并将其添加到表中

data['hour']=data.Time.apply(lambda x:int(x.split(':')[0]))
现在我想把时间分成一天中的3部分和3次用餐时间,假设时间间隔
(6,11]
是早餐,
(11,15]
是午餐,
(15,20]
是晚餐

我试过这个代码,但似乎不起作用

def时间段(小时):
如果小时>=6且小时<11:
返回“早餐”
elif小时>=11和小时<15:
返回“午餐”
其他:
返回“晚餐”
时间=[以小时为单位的y的时间段(y)]
日期['用餐]=时间
使用
pandas.apply()

data=pd.DataFrame({'time':['13:30','9:20','18:12','19:00','8:20']})
data['hour']=data['time'].apply(lambda x:int(x.split(':')[0]))
def时间段(小时):
如果小时>=6且小时<11:
返回“早餐”
elif小时>=11和小时<15:
返回“午餐”
其他:
返回“晚餐”
数据['MEIN']=数据['hour'].应用(λx:时间段(x))
输出

time    hour    meal
13:30   13  lunch
9:20    9   breakfast
18:12   18  dinner
19:00   19  dinner
8:20    8   breakfast
使用:

使用链接:

>>> data.assign(
        meal=pd.Series(
            ['breakfast', 'lunch', 'dinner'], 
            index=pd.IntervalIndex.from_breaks([6, 11, 15, 20], closed='right')
        ).loc[pd.to_datetime(data.time, format='%H:%M').dt.hour].to_numpy()
    )

    time       meal
0  13:30      lunch
1   9:20  breakfast
2  18:12     dinner
3  19:00     dinner
4   8:20  breakfast

错误是什么?这么多的代码输入我可能是错的,但我认为这段代码是不完整的。
hours
,在
time=[time\u period(y)for y in hours]
中使用的是什么?
>>> data.assign(
        meal=pd.Series(
            ['breakfast', 'lunch', 'dinner'], 
            index=pd.IntervalIndex.from_breaks([6, 11, 15, 20], closed='right')
        ).loc[pd.to_datetime(data.time, format='%H:%M').dt.hour].to_numpy()
    )

    time       meal
0  13:30      lunch
1   9:20  breakfast
2  18:12     dinner
3  19:00     dinner
4   8:20  breakfast