Search code examples
pythonpython-3.xpandaspandasql

Format datetime field as MON-YYYY in python pandas & ignore nulls


I have a python pandas dataframe "df" as below -

      NAME  SETID   VENDOR_ID vendor_created_date
0  Vendor1     SD          93 2002-11-22 11:04:33
1  Vendor2     SD          94 2003-08-09 11:40:59
2  Service1    SD          95 2003-10-31 10:29:21
3  Vendor3     SD          01                 NaT
4  Vendor4     SD          02                 NaT 

The vendor_created_date is of the format datetime64[ns].

Now I want to create a new field called fomatted_date where the vendor_created_date field values should be in the format MON-YYYY & I want to delete the rows with NaT in the date field.

Can you please give me directions?


Solution

  • is this the format you wanted?

    Basically we can drop the NaN rows first and then call apply and use datetime.strftime to apply a new format:

    In [24]:
    
    df = df.dropna()
    df['fomatted_date'] = df['vendor_created_date'].apply(lambda x: dt.datetime.strftime(x,'%b-%Y'))
    df
    Out[24]:
               NAME SETID  VENDOR_ID vendor_created_date fomatted_date
    Index                                                             
    0       Vendor1    SD         93 2002-11-22 11:04:33      Nov-2002
    1       Vendor2    SD         94 2003-08-09 11:40:59      Aug-2003
    2      Service1    SD         95 2003-10-31 10:29:21      Oct-2003