在Python中使用Pandas进行数字到日期的转换? [英] Number to Date Conversion using Pandas in Python?
本文介绍了在Python中使用Pandas进行数字到日期的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试从数字格式转换为日期时,得到的结果与在Excel中获得的结果不同.
When I try to convert from number format to Date I'm not getting the same result what I get in Excel.
我需要将数字转换为日期格式,并获得与Excel中相同的结果.
I need to convert a Number to date format and get the same result what I get in Excel.
以Excel中的以下数字为例,我得到以下信息:
For Example in Excel for the below Number I get the following:
Input - 42970.73819
Output- 8/23/2017 17:43
我尝试在Pandas中使用日期转换,但没有得到与 Excel 相同的结果.
I tried using the date conversion in Pandas but not getting the same result as of Excel.
谢谢 马丹
推荐答案
我认为您需要转换序列号:
df = pd.DataFrame({'date':[42970.73819,42970.73819]})
print (df)
date
0 42970.73819
1 42970.73819
df = pd.to_datetime((df['date'] - 25569) * 86400.0, unit='s')
print (df)
0 2017-08-23 17:42:59.616
1 2017-08-23 17:42:59.616
Name: date, dtype: datetime64[ns]
这篇关于在Python中使用Pandas进行数字到日期的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文