Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number to Date Conversion using Pandas in Python?

When I try to convert from number format to Date I'm not getting the same result what I get in Excel.

I need to convert a Number to date format and get the same result what I get in Excel.

For Example in Excel for the below Number I get the following:

Input - 42970.73819
Output- 8/23/2017 17:43

I tried using the date conversion in Pandas but not getting the same result as of Excel.

Thank you Madan

like image 311
Madan Kumar Avatar asked Oct 31 '25 17:10

Madan Kumar


1 Answers

I think you need convert serial date:

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]
like image 164
jezrael Avatar answered Nov 03 '25 09:11

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!