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 使用pandas解析从一年中的一周到datetime对象_Python_Pandas_Datetime - Fatal编程技术网

Python 使用pandas解析从一年中的一周到datetime对象

Python 使用pandas解析从一年中的一周到datetime对象,python,pandas,datetime,Python,Pandas,Datetime,最后一列包含年份和周数。是否可以将其转换为带有pd.to_datetime的datetime列 我试过: A B C D yearweek 0 245 95 60 30 2014-48 1 245 15 70 25 2014-49 2 150 275 385 175 2014-50 3 100 260 170 335 2014-51 4 580 925 535 2590 2015-02 5 63

最后一列包含年份和周数。是否可以将其转换为带有
pd.to_datetime
的datetime列

我试过:

     A    B    C     D yearweek
0  245   95   60    30  2014-48
1  245   15   70    25  2014-49
2  150  275  385   175  2014-50
3  100  260  170   335  2014-51
4  580  925  535  2590  2015-02
5  630  126  485  2115  2015-03
6  425   90  905  1085  2015-04
7  210  670  655   945  2015-05

但是输出是不正确的,我认为%U应该是周数的格式字符串。这里缺少什么?

您需要另一个参数来指定日期-检查:

pd.to_datetime(df.yearweek, format='%Y-%U')

0   2014-01-01
1   2014-01-01
2   2014-01-01
3   2014-01-01
4   2015-01-01
5   2015-01-01
6   2015-01-01
7   2015-01-01
Name: yearweek, dtype: datetime64[ns]
df = pd.to_datetime(df.yearweek.add('-0'), format='%Y-%W-%w')
print (df)
0   2014-12-07
1   2014-12-14
2   2014-12-21
3   2014-12-28
4   2015-01-18
5   2015-01-25
6   2015-02-01
7   2015-02-08
Name: yearweek, dtype: datetime64[ns]