[Python] What is the oldest leap year in pandas?

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with a day of year column that ranges from 1 to 366 (to account for leap years). I need to convert this column into a date for a specific task and I would like to set it to a year that is very unlikely to appear in my time series.

    Is there a way to set it to the oldest leap year of pandas?

    import pandas as pd

    # here is an example where the data have already been converted to datetime object
    # I just missed the year to set
    dates = pd.Series(pd.to_datetime(['2023-05-01', '2021-12-15', '2019-07-20']))
    first_leap_year = 2000 # this is where I don't know what to set
    new_dates = dates.apply(lambda d: d.replace(year=first_leap_year))

    Continue reading...

Compartilhe esta Página