Pandas Lambda Function Format Month and Day

Viewed 1727

I have a DF "ltyc" that looks like this:

month  day  wind_speed
0      1    1   11.263604
1      1    2   11.971495
2      1    3   11.989080
3      1    4   12.558736
4      1    5   11.850899

And, i apply a lambda function:

ltyc['date'] = pd.to_datetime(ltyc["month"], format='%m').apply(lambda dt: dt.replace(year=2020))

To get it to look like this:

month  day  wind_speed       date
0      1    1   11.263604 2020-01-01
1      1    2   11.971495 2020-01-01
2      1    3   11.989080 2020-01-01
3      1    4   12.558736 2020-01-01
4      1    5   11.850899 2020-01-01

Except, I need it to look like this so that the days change also...but I cannot figure out how to format the lambda statement to do this instead as this is what I need.

month  day  wind_speed       date
0      1    1   11.263604 2020-01-01
1      1    2   11.971495 2020-01-02
2      1    3   11.989080 2020-01-03
3      1    4   12.558736 2020-01-04
4      1    5   11.850899 2020-01-05

I have tried this:

ltyc['date'] = pd.to_datetime(ltyc["month"], format='%m%d').apply(lambda dt: dt.replace(year=2020))

and i get this error:

ValueError: time data '1' does not match format '%m%d' (match)

Thank you for help since i'm trying to figure out the lambda functions.

3 Answers

create a series with value 2020 and name year. Concat it to ['month', 'day'] and passing to pd.to_datetime. As long as, you passing a dataframe with columns names in this order year, month, date, pd.to_datetime will convert it to the appropriate datetime series.

@Allolz suggestion:

ltyc['date'] = pd.to_datetime(ltyc[['day', 'month']].assign(year=2020))

Out[367]:
   month  day  wind_speed       date
0      1    1   11.263604 2020-01-01
1      1    2   11.971495 2020-01-02
2      1    3   11.989080 2020-01-03
3      1    4   12.558736 2020-01-04
4      1    5   11.850899 2020-01-05

Or you may use reindex to create the sub-dataframe to pass to pd.to_datetime

ltyc['date'] = pd.to_datetime(ltyc.reindex(['year','month','day'], 
                                           axis=1, fill_value=2020))

Original:

s = pd.Series([2020]*len(ltyc), name='year')
ltyc['date'] = pd.to_datetime(pd.concat([s, ltyc[['month','day']]], axis=1))

This is similar to a previous answer, but does not persist the 'helper' column with the year. In brief, we pass a data frame with three columns (year, month, day) to the to_datetime() function.

ltyc['date'] = pd.to_datetime(ltyc
                              .assign(year=2020)
                              .filter(['year', 'month', 'day'])
                             )

You could also use your method and add month and day together with .astype(str) and then add %d to the format. The problem with your lambda is that you only considered month, so this is how you would consider month and day.

ltyc['date'] = (pd.to_datetime(ltyc["month"].astype(str) + '-' + ltyc["day"].astype(str),
                               format='%m-%d')
                .apply(lambda dt: dt.replace(year=2020)))

output:

    month   day wind_speed  date
0   1       1   11.263604   2020-01-01
1   1       2   11.971495   2020-01-02
2   1       3   11.989080   2020-01-03
3   1       4   12.558736   2020-01-04
4   1       5   11.850899   2020-01-05
Related