Search code examples
pythondatedata-cleaningdays

how to convert date in dd/mm/yyyy to days in python csv


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


Solution

  • 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