Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'Timestamp' object has no attribute 'timestamp'

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
like image 246
Jørgen Hanson Mathiesen Avatar asked Nov 07 '16 19:11

Jørgen Hanson Mathiesen


3 Answers

You can try this:

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

That worked for me!

like image 103
Mikebarson Avatar answered Oct 24 '22 03:10

Mikebarson


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()
like image 2
derasd Avatar answered Oct 24 '22 05:10

derasd


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).

like image 1
maxkoryukov Avatar answered Oct 24 '22 04:10

maxkoryukov