1

I'm trying to convert these dates to the number type, but I'm not able to do it.
Input:

import pandas as pd
import datetime as dt

new_df = { 'date': [ '20/08/2008', '21/08/2008','22/08/2008'  ], 'valor': ['a','b','c'] }
pd.DataFrame(new_df)

Example:

    date        valor
0   20/08/2008  a
1   21/08/2008  b
2   22/08/2008  c

Expected:

    date    valor
0   39680   a
1   39681   b
2   39682   c
imxitiz
  • 3,920
  • 3
  • 9
  • 33
Zan
  • 159
  • 1
  • 1
  • 10
  • 1
    See: https://stackoverflow.com/questions/9574793/how-to-convert-a-python-datetime-datetime-to-excel-serial-date-number – slothrop Jul 15 '22 at 19:19
  • If you are going to deal with time in python please invest some time learning about the datetime class before trying to ask questions on stackoverflow: https://docs.python.org/3/library/datetime.html – Andrew Allaire Jul 15 '22 at 19:22
  • I've already looked at the documentation there's nothing about it, ask before judging :) – Zan Jul 15 '22 at 19:35
  • 1
    The logic is unclear, you can't simply convert dates to integers without knowing the wanted logic. What is it? Why 39680? A wild guess would be the number of days since 1900-01-01 but it's off by 2 days. – mozway Jul 15 '22 at 19:41
  • It looks like you're trying to convert to Excel's date format, so I've closed your question as a duplicate. If you're actually trying to do something subtly different, LMK and I can reopen. For the sake of future questions, please read [ask], which has tips like showing us what you've already tried and starting with your own research. I found that question by googling `pandas convert date to excel number`. – wjandrea Jul 15 '22 at 20:22

1 Answers1

0

Assuming (wild guess), that you want the number of days since 1899-12-30, you could use:

df['diff'] = (pd.to_datetime(df['date'], dayfirst=True)
             -pd.Timestamp('1899-12-30')).dt.days

Output:

         date valor   diff
0  20/08/2008     a  39680
1  21/08/2008     b  39681
2  22/08/2008     c  39682
mozway
  • 194,879
  • 13
  • 39
  • 75