FormidableData
FormidableData

Reputation: 65

Replace day of a date in a dataframe column

I have created a dataframe with a range of dates from 8/1/18 to today() and am trying to assign a month_start_date to each date (eg: 2018/08/04 would be 2018/08/01).

I have been able to get the day of the date into month_start_date, but I'm really just trying to replace the day in the date column with 1 for all dates.

import pandas as pd
from datetime import datetime

datelist = pd.date_range(start='2018-08-01', end=datetime.today())

df_columns = ['date']
df = pd.DataFrame(datelist, columns = df_columns)

df['month_start_date'] = df['date'].dt.day
print(df)
          date  month_start_date
0   2018-08-01                 1
1   2018-08-02                 2
2   2018-08-03                 3
3   2018-08-04                 4
4   2018-08-05                 5

Upvotes: 2

Views: 3725

Answers (2)

njriasan
njriasan

Reputation: 71

You can do this more generally with the Pandas.tseries.offsets package. In this example you can calculate your dates using MonthBegin

import datetime
import pandas as pd

datelist = pd.date_range(start='2018-08-01',end=datetime.datetime.today())
month_start_list = (datelist + datetime.timedelta(1)) + pd.tseries.offsets.MonthBegin(n=-1)
df = pd.DataFrame({"date": datelist, "month_start": month_start_list})
print(df)
          date month_start
0   2018-08-01  2018-08-01
1   2018-08-02  2018-08-01
2   2018-08-03  2018-08-01
3   2018-08-04  2018-08-01
4   2018-08-05  2018-08-01
..         ...         ...
892 2021-01-09  2021-01-01
893 2021-01-10  2021-01-01
894 2021-01-11  2021-01-01
895 2021-01-12  2021-01-01
896 2021-01-13  2021-01-01

[897 rows x 2 columns]

Upvotes: 2

buran
buran

Reputation: 14233

df['month_start_date'] = pd.to_datetime(df['date']).apply(lambda x: x.replace(day=1))

Upvotes: 4

Related Questions