I am getting this error in my script
'str' object has no attribute 'strftime'
df.set_index('Profile_ID', inplace=True)
df['CohortGroup'] = df.groupby(level=0)['Date_of_Service_Requested'].min().apply(lambda x: x.strftime('%Y-%m'))
df.reset_index(inplace=True)
df.head()
strftime
is a datetime
method, not a str
method. You can create a datetime
series using pd.to_datetime
:
import pandas as pd
df = pd.DataFrame({'date_column': ['2019-03-15', '2016-12-05'],
'other_column': [10, 4]})
df['date_column'] = pd.to_datetime(arg=df['date_column'],
format='%Y-%m-%d')
df['date_column'].apply(lambda x: x.strftime('%Y-%m'))