0

I need to convert TimeStamp entry to simple dd mm yyyy format in a dataframe as I need to apply GroupBy at some later stage. Issue is, the format is simply not applying. My code is

from __future__ import division
import urllib.request
import csv
import pandas as pd
import datetime as dt

def changeFormat(inputFormat):
    return pd.to_datetime(inputFormat, format='%Y%m%d')

df = pd.read_excel('Incidents.xlsx',encoding='ISO-8859-1')
df = df.sort_values('REGISTERED_DT#CST')
##df.info()
columnsToExt = ['REGISTERED_DT#CST', 'PRIORITY']
df1 = pd.DataFrame(df, columns=columnsToExt)

df1['REGISTERED_DT#CST'] = df1['REGISTERED_DT#CST'].apply(lambda x: changeFormat(x))

##by_date = df1.groupby('REGISTERED_DT#CST')

print(df1.head())

My output is still the same as if function never got applied to it:

    REGISTERED_DT#CST  PRIORITY
0 2015-06-01 00:22:17         5
1 2015-06-01 06:42:27         4
2 2015-06-01 06:42:59         5
3 2015-06-01 07:00:03         5
4 2015-06-01 07:15:04         4
>>>

I want it in below format:

    REGISTERED_DT#CST  PRIORITY
0 2015-06-01                  5
1 2015-06-01                  4
2 2015-06-01                  5
3 2015-06-01                  5
4 2015-06-01                  4
>>>   
Rohit
  • 145
  • 2
  • 11
  • 2
    Sorry you want just the date or you want string representation? if you just want the date you can do `df1['REGISTERED_DT#CST'].dt.date` to get just the date – EdChum Jul 21 '16 at 10:27
  • Oh my. I did not knew about this, i wanted the date itself. How do I mark comment as answer ? – Rohit Jul 21 '16 at 10:30
  • No need this is therefore a duplicate, see dupe link, you can upvote the linked answer if you wish – EdChum Jul 21 '16 at 10:32
  • To explain why what you tried didn't work, it's possible it was already a datetime as `read_excel` just sniffs the `dtypes` from `excel` if you look at `df.info()` after `read_excel` it probably show's it's already a datetime, also the displayed format is your locale preferred display format for datetime, if you wanted to change the displayed format you have to add a new column of the string representation e.g. `df['new_col'] = df['REGISTERED_DT#CST'].dt.strftime('%d-%m-%Y')` but this will be a string and not that useful IMO – EdChum Jul 21 '16 at 10:38

0 Answers0