Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 - Fatal编程技术网

Python 将月份号转换为日期时间(单位:秒)

Python 将月份号转换为日期时间(单位:秒),python,pandas,Python,Pandas,如何使用类似于2010-01-01的格式将熊猫中的月数(float)转换为日期时间 date 0 1.0 1 2.0 2 3.0 3 4.0 4 5.0 预期输出: date 0 2010-01-01 1 2010-02-01 2 2010-03-01 3 2010-04-01 4 2010-05-01 一个简单的解决方案是只需

如何使用类似于2010-01-01的格式将熊猫中的月数(float)转换为日期时间

    date
0   1.0       
1   2.0       
2   3.0       
3   4.0       
4   5.0 
预期输出:

    date
0   2010-01-01       
1   2010-02-01      
2   2010-03-01      
3   2010-04-01       
4   2010-05-01

一个简单的解决方案是只需将
year
day
列添加到数据框中,然后调用
pd.to\u datetime(df)


追加年和月并转换为日期时间

pd.to_datetime('2010-' + df.date.astype(int).astype(str) + '-1', format = '%Y-%m')

0   2010-01-01
1   2010-02-01
2   2010-03-01
3   2010-04-01
4   2010-05-01

今年是2010年,有什么特别的原因吗?如果你知道你的年份是2010年,你可以先加上这个字符串。df.date=pd.to_datetime('2010-'+df.date.astype(str),格式='%Y-%m')这是我需要的年份@Chrishow我可以添加日期吗@默认情况下,VaishaliPandas将以1作为日期,否则您也可以连接该日期。pd.to_datetime('2010-'+df.date.astype(str)+'-1',format='%Y-%m')必须重新格式化我的问题,它是一个浮点而不是一个整数。@Pepe编辑的答案。@ernest_k你能帮助下面的问题吗?你能帮助下面的问题吗
pd.to_datetime('2010-' + df.date.astype(int).astype(str) + '-1', format = '%Y-%m')

0   2010-01-01
1   2010-02-01
2   2010-03-01
3   2010-04-01
4   2010-05-01