0

I have multiple datettime columns in my dataframe and when I export the datetime to csv, I need to convert the datetime from Month/Day/Year to Month/Year. Is it possible to do this?

I was trying this:

if date_mask == "MMM":
    df[name].apply(lambda x: x.strftime('%b %Y'))
else:
    df[name].apply(lambda x: x.strftime('%m %Y'))

When I look at the exported CSV I still see the old datetime values.

Any ideas on how to do this?

Thanks

##########################################################

Solution

def modify_date(x):
    try:
        if pd.isnull(x) == False:
            return x.strftime('%b %Y')
        else:
            print pd.NaT
    except:
        return pd.NaT

df = pd.DateFrame.from_records(<some list from database>)

df[name] = df[name].apply(modify_date)

Thanks for all the help!

code base 5000
  • 3,812
  • 13
  • 44
  • 73

1 Answers1

0

Solution

def modify_date(x):
    try:
        if pd.isnull(x) == False:
            return x.strftime('%b %Y')
        else:
            print pd.NaT
    except:
        return pd.NaT

df = pd.DateFrame.from_records(<some list from database>)

df[name] = df[name].apply(modify_date)

Thanks for all the help!

code base 5000
  • 3,812
  • 13
  • 44
  • 73