-1

i will like to covert dates to days

Example 01/01/2001 should be Day 1, 02/01/2001 should be Day 2

I have tried

prices_df['01/01/2001'] = prices_df['days'].dt.days

Dera
  • 13
  • 2

1 Answers1

0

Without using any external function or a complicated solution, you can simply take the first 2 chars of the string.

int('01/01/2001'[0:2])

Output:

1

If you have to do this in a pandas column:

import pandas as pd
pd.to_numeric(df['days'].str[0:2])

N.B. This works if all date are in the form day/month/year

Will
  • 1,619
  • 5
  • 23
  • Hi Will, I have tried this but this did not woork for me. i need a fuction to count the total number of days in the 'days' column between a start date of 1st Jan 1995 and an end date of 31st Dec 2019 in a dataframe taking leapyears into account as well. Example: Ist Jan 1995 - Day 1, 1st Feb 1995 - Day 32 .......and so on all the way to 31st Dec 2019. – Dera Nov 07 '22 at 16:53
  • @Dera this is another question... I answered for your previous question. Vote my answer as correct. Then open a new question and send me the link to the new question. – Will Nov 07 '22 at 19:09
  • Hi Will, it's my first day using stack overflow so i wasn't aware i needed to vote, now i know. Thanks – Dera Nov 07 '22 at 21:57
  • 1
    here is the link to the question: https://stackoverflow.com/questions/74353426/how-to-convert-date-format-dd-mm-yyyy-to-days-in-python-csv – Dera Nov 07 '22 at 22:07