0

How do you convert a pandas dataframe column from a date formatted as below to a number as shown below:

        date 
0 4/5/2010     
1 9/26/2014     
2 8/3/2010     

To this

        date newFormat
0 4/5/2010     40273
1 9/26/2014    41908
2 8/3/2010     40393

Where the second columns is the number of days since 1/1/1900.

aise0603
  • 11
  • 4
  • Does this answer your question? [How to convert a given ordinal number (from Excel) to a date](https://stackoverflow.com/questions/29387137/how-to-convert-a-given-ordinal-number-from-excel-to-a-date) – Andreas May 04 '21 at 13:13
  • To apply to a Series of dates you would: `(pd.to_datetime(df['date'], format='%m/%d/%Y')-pd.to_datetime('1900-01-01')).dt.days` – ALollz May 04 '21 at 13:16

1 Answers1

0

Use:

data['newFormat'] = data['Date'].dt.strftime("%Y%m%d").astype(int)

This has been answered before: Pandas: convert date 'object' to int enter link description here

Julia
  • 981
  • 1
  • 8
  • 16
  • Does this result in the number of days since 1/1/1900? – jarmod May 04 '21 at 13:16
  • @jarmod It does not, this is a bad copy-paste from a different question and does not answer OP's question at all. This results in 01/01/1970 being converted into the integer 01011970, which is very different from what OP is asking – Barbaud Julien Jul 04 '23 at 10:48