Slartibartfast
Slartibartfast

Reputation: 1190

Changing datetime format in py

I have the following code which gives the following output:

print(df1['Diff'].mean())

outputs:

10 days 16:13:29.467455

But since i just want the days value and not the time, i have done this:

print(datetime.strptime(df1['Diff'].mean(), format ='%d')

but i am getting the following error:

                    ^
SyntaxError: unexpected EOF while parsing

Why am i getting this error?

Upvotes: 0

Views: 35

Answers (2)

Akash Badam
Akash Badam

Reputation: 476

I think the instance of df1['Diff'].mean() is str and datetime.strptime() can be use only in datetime methods. So to only get date you have to take slice of df1['Diff'].mean() like df1['Diff'].mean()[:-14] Which is in your case.

Upvotes: 1

Sunny Patel
Sunny Patel

Reputation: 8077

For date, time, and datetime objects

You should be using strftime to format the time, not to parse the time (as in strptime).

print(obj.strftime('%d'))

strptime expects a string to be passed in (and you were passing in a datetime object), whereas strftime formats an existing datetime object.

For timedelta objects

print(obj.days)

This gets the days counterpart you're looking for.

Upvotes: 1

Related Questions