6

I am struggling to get my code running. There seems to be a problem with the timestamp. Do you have any suggestions to how I could change my code? I saw that this had been asked before, but did not manage to make it work.

This is the error I get when running the code: 'Timestamp' object has no attribute 'timestamp'

My code:

import quandl, math, datetime

last_date = df.iloc[-1].name
last_unix = last_date.timestamp()
one_day = 86400 #Seconds in a day
next_unix = last_unix + one_day


for i in forecast_set: 
    next_date = datetime.datetime.fromtimestamp(next_unix)
    next_unix += one_day
    df.loc[next_date]=[np.nan for _ in range(len(df.columns)-1)]+[i]
    #Loop to replace all numbers on x axis with dates
3
  • 1
    "saw that this had been asked before" - where? "but did not manage to make it work" - be more specific. Commented Nov 7, 2016 at 19:58
  • I agree with jonrsharpe, and please clean your code so it is an (else) working example. Is df a pandas data frame? Then you might add pandas as a tag. Where does it fail? - I guess in last_date.timestamp()? Then the rest of the code would not be relevant here. Commented Nov 7, 2016 at 20:07
  • You might have a look at github.com/pandas-dev/pandas/issues/17329 Commented Aug 24, 2017 at 19:55

3 Answers 3

6

You can try this:

import time
.....
last_unix = time.mktime(last_date.timetuple())

That worked for me!

Sign up to request clarification or add additional context in comments.

5 Comments

Nice! This is the correct answer. @Mikebarson Do you know why?
@Mr.Concolato Hi! It's been a while since I've posted this answer. And I'm so sorry, but right now I've forgotten why this worked. If you can help me remember that would be great!
Not completely sure, but I think it has something to do with the version (2.7 vs. 3.0) of python you are running. The above answer worked for me in 2.7.
@Mr.Concolato I see. I'll check this out soon and hopefully give a more detailed explanation as to why it worked.
It was the correct way to obtain the timestamp in the qpython interpreter running on an Android smartphone. Currently, the qpython environment executes python 3.2, which causes this kind of headache !
2

I had the same problem but with Python 3.4 and I used the following solution.

last_unix = (last_date - datetime.datetime(1970,1,1)).total_seconds()

Comments

1

I've had the same problem. I've needed to provide an access to timestamp in both: python2 and python3 environments (timestamps wasn't the main purpose of my module).

After several attempts to provide compatibility, I have installed arrow package. It is already tested, pypi-published, and it works on many version of Python.

Install

Install from PyPI

pip install arrow

Or add to dependencies

Use in code

import arrow
import quandl, math, datetime

last_date = df.iloc[-1].name
# use arrow
last_unix = arrow.get(last_date).timestamp
one_day = 86400 #Seconds in a day
next_unix = last_unix + one_day

# ... your code ...

Also, there are a lot of nifty and useful features (for example - no more tortures with time zones).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.