-1

I have a bunch of dates as follows:

df['Execution Date']
Out[123]: 
214     20180420
215     20180420
256     20180423
258     20180424
262     20180425
273     20180508
274     20180510
275     20180510
278     20180511
281     20180511
284     20180511
287     20180511
290     20180511
293     20180511
296     20180511
333     20180516

I implement df['Execution Date'] = df['Execution Date'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d'))

When I check my console, the reformatting seems correct but in my Spyder Variable Explorer pane, I've the unnecessary 00:00:00 after each YYYY-MM-DD.

This affects readability. Is there any workaround?

srkdb
  • 775
  • 3
  • 15
  • 28
  • 1
    This question should be able to help you: https://stackoverflow.com/questions/45858155/removing-the-timestamp-from-a-datetime-in-pandas-dataframe – Riebeckite May 02 '19 at 17:25

1 Answers1

2

use df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d')) as below

d = {'Execution Date' : ['20180420','20180420', '20180423']}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(x,'%Y%m%d'))
print(df)

result is

  Execution Date
0     2018-04-20
1     2018-04-20
2     2018-04-23
[Finished in 1.2s]

if the type of 'Execution Date' is int,then cast to string as follows.

d = {'Execution Date' : [20180420,20180420, 20180423]}
df = pd.DataFrame(d)
df['Execution Date'] = df['Execution Date'].apply(lambda x: datetime.strptime(str(x),'%Y%m%d'))
print(df)
Prince Francis
  • 2,995
  • 1
  • 14
  • 22
  • I get this error: TypeError: strptime() argument 1 must be str, not int – srkdb Jun 11 '19 at 20:15
  • 1
    That means, the type of 'Execution Date' in the original dataframe is int. Cast it into string process. I have updated the answer. Please check. – Prince Francis Jun 12 '19 at 06:01