Search code examples
pandasdataframelambdaapplyseries

apply lambda function error for date issue


I am trying to extract month from the below join_date. below is the structure of the emp table. and i am getting an error while performing the below code:

emp['join_mth']=emp['join_date'].apply(lambda x:x[:7])

emp_id emp_name account_id join_date
1       rob     121         2019-01-01
2       sam     122         2019-02-02
3       mike    123         2019-03-03
4       tom     124         2019-04-04 

type(emp['join_date'])
<class 'pandas.core.series.Series'>

emp.dtypes
emp_id          object
emp_name        object  
account_id      object
join_date       object
dtype:object

fail to excute line - 10: emp['join_mth']=emp['join_date'].apply(lambda x:x[:7])

Below is the exact error:

Traceback (most recent call last):
  File "<stdin>", line 39, in <module>
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/lib.pyx", line 2467, in pandas._libs.lib.map_infer
  File "<stdin>", line 39, in <lambda>
TypeError: 'datetime.date' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 70, in <module>
AttributeError: module 'sys' has no attribute 'last_value'

Solution

  • read your error carefully it says: "'datetime.date' object is not subscriptable" so your 'join_date' is of dtype datetime.date so use typecast it to string first:

    emp['join_mth']=emp['join_date'].astype(str).str[:7]
    #OR
    emp['join_mth']=emp['join_date'].astype(str).apply(lambda x:x[:7])
    

    OR

    Since it's of type datetime.date so you can also use:

    emp['join_date']=[x.strftime("%Y-%m") for x in emp['join_date']]
    #OR
    emp['join_mth']=emp['join_date'].map(lambda x:x.strftime("%Y-%m"))
    

    OR

    If you only want to extract month then use:

    emp['join_date']=[x.strftime("%m") for x in emp['join_date']]
    #emp['join_date'].apply(lambda x:x.strftime("%m"))
    #OR(use above code for string format and below for int format)
    emp['join_date']=[x.month for x in emp['join_date']]
    #emp['join_date'].map(lambda x:x.month)