1

I have a dataframe which datatype is float64 and I want to change it to datetime 64. But the result is return to only one day : 1970-01-01 no matter which method I use. Any help please

df.product_first_sold_date = [41245,0, 37659.0,40487.0,41701.0,40649.0]
dt.cv = pd.to_datetime(df.product_first_sold_date)
dt.cv
dt.cv2 = df.product_first_sold_date.apply(lambda x: datetime.fromtimestamp(x).strftime('%m-%d-%Y') if x==x else None)
dt.cv2

enter image description here

7
  • 2
    What's your expected date for 41245? Commented Jun 5, 2020 at 4:40
  • 1
    Add sample data as text format. do not add image. Commented Jun 5, 2020 at 4:42
  • Check this - stackoverflow.com/q/49956741/6075699 Commented Jun 5, 2020 at 4:45
  • @QuangHoang: as I check in excel , the expected date for 41245 is 2012-12-02 Commented Jun 5, 2020 at 4:54
  • @Suzie see my answer below. Commented Jun 5, 2020 at 4:56

1 Answer 1

4

I believe you re dealing with Excel date type which is the number of days since 1900-01-01, as @Dishin pointed out 1899-12-30

# sample data:
df = pd.DataFrame({'date':[41245,37659,40487]})

# convert - adjust 1900-01-01 to the correct day
df['date'] = pd.to_timedelta(df.date, unit='D') + pd.to_datetime('1899-12-30')

Output:

        date
0 2012-12-02
1 2003-02-07
2 2010-11-05
Sign up to request clarification or add additional context in comments.

2 Comments

I think OLE start from "30 December 1899"
So 41245 is suppose to be "2012-12-02"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.