Arthur Vott
Arthur Vott

Reputation: 25

Timestamp Issue: Python doesn't recognize Timestamp

so I am pretty new to coding, but seriously I do not see where I am heading wrong with my code.

So what I want to do is an operation where I subtract 365 days from a specific day. Thats the easy part. But when I start to import my array with all the timestamps - it doesn't recognize them. Funnily if I just type in the timestamp by hand, it does!!

What I wrote looks like this:

r= '2016-12-22 00:00:00'
u = datetime.strptime(r,"%Y-%m-%d %H:%M:%S")
d = timedelta(days=365)
print (u-d)

The result:

2015-12-23 00:00:00

But when I use an indexing command such as:

r= p_dates[254] 

which is EXACTLY the same as:

print(p_dates[254])
2016-12-22 00:00:00

It returns an Error:

strptime() argument 1 must be str, not Timestamp

Seriously - what the heck is happening :D Would be glad, if somebody can help a noob out.

Thanks in advance and happy holidays

Upvotes: 1

Views: 428

Answers (1)

Alam
Alam

Reputation: 403

It says that the first argument needs to be of str type. You can check the type of any variable like type(variable). Here just cast to a string from a timestamp

u = datetime.strptime(str(r),"%Y-%m-%d %H:%M:%S")

Upvotes: 1

Related Questions