[Python] Python Validate Date Format and Skip Non Dates

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    This can be run as written. I would like to validate and format row 1 -> "2023-05-26" and pass/skip text row 2 ->"6/28/23-6/30/23". I am a novice at this and how to ask. I would like an FORMAT ONLY IN ROW 1 with backslash-> "5/26/2023" and SKIP/Do NOTHING to row 2 ->"6/28/23-6/30/23". I CANNOT GET ROW 1 to format mm/dd/yyyy. ROW 1 and ROW 2 as just 2 example of ROWS. There are numerous rows. What I am doing incorrect?

    CODE::

    #Dataframe number of rows
    num_rows = len(df_merge)
    def validate_date():
    for i in range(num_rows):
    try:
    df_merge['DOS'] = pd.to_datetime(df_merge['DOS']).dt.strftime('%m/%d/%Y')
    except (ValueError, TypeError):
    pass

    Continue reading...

Compartilhe esta Página