Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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 使用Windows解析熊猫中的单个十进制小时_Python_Pandas_Strftime - Fatal编程技术网

Python 使用Windows解析熊猫中的单个十进制小时

Python 使用Windows解析熊猫中的单个十进制小时,python,pandas,strftime,Python,Pandas,Strftime,我得到了以下数据帧: date hour 20140101 0 20140101 1 20140101 2 20140101 3 20140101 4 ... ... 我想将这两个列组合成一个datetime列。我尝试了以下操作,但我发现一个错误,因为我使用的是Windows,我认为: wdf['date'] = wdf['date'].astype(str) + ' ' + wdf['hour'].a

我得到了以下数据帧:

date         hour  
20140101     0 
20140101     1 
20140101     2  
20140101     3   
20140101     4
...          ...
我想将这两个列组合成一个datetime列。我尝试了以下操作,但我发现一个错误,因为我使用的是Windows,我认为:

wdf['date'] = wdf['date'].astype(str) + ' ' + wdf['hour'].astype(str)
wdf['date'] = pd.to_datetime(wdf['date'], format='%Y%m%d %-H')
ValueError:“-”是格式为“%Y%m%d%H”的错误指令

我尝试了
%#H
,但这导致了类似的错误。我必须使用破折号或磅号,因为小时符号是一个十进制。

我会尝试在需要的地方使用
0
填充,并使用常规的
%H

试试这个:

wdf['date']=wdf['date'].astype(str)+''+wdf['hour'].astype(str).str.zfill(2)
wdf['date']=pd.to_datetime(wdf['date'],格式=“%Y%m%d%H”)
输出:

                 date  hour
0 2014-01-01 00:00:00     0
1 2014-01-01 01:00:00     1
2 2014-01-01 02:00:00     2
3 2014-01-01 03:00:00     3
4 2014-01-01 04:00:00     4