Issue
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?
Solution
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)
Answered By - Prince Francis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.