Bolajio Bolajio - 3 months ago 46
Python Question

How do i work out this date time issue in python

I am new to Python and machine learning and i am trying to work out how to fix this issue with date time. next_unix is 13148730, because that is how many seconds are in five months, which is the time in between my dates. I have searched and i can't seem to find anything that works.

last_date = df.iloc[1,0]
last_unix = pd.to_datetime('2015-01-31 00:00:00') +pd.Timedelta(13148730)
five_months = 13148730
next_unix = last_unix + five_months


for i in forecast_set:
next_date = Timestamp('2015-06-30 00:00:00')
next_unix += 13148730
df.loc[next_date] = [np.nan for _ in range(len(df.columns)-1)]+[i]


Error:
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/HP/Documents/machine learning.py', wdir='C:/Users/HP/Documents')

File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)

File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/HP/Documents/machine learning.py", line 74, in
next_unix = last_unix + five_months

File "pandas\tslib.pyx", line 1025, in pandas.tslib._Timestamp.add (pandas\tslib.c:20118)

ValueError: Cannot add integral value to Timestamp without offset.

Answer

you can use pd.Timedelta() method:

In [20]: pd.to_datetime('2015-01-31 00:00:00') + pd.Timedelta(seconds=13148730)
Out[20]: Timestamp('2015-07-02 04:25:30')

if we try to add number of seconds to Pandas DateTime:

In [22]: pd.to_datetime('2015-01-31 00:00:00') + 13148730
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-c252fc51ec14> in <module>()
----> 1 pd.to_datetime('2015-01-31 00:00:00') + 13148730

pandas\tslib.pyx in pandas.tslib._Timestamp.__add__ (pandas\tslib.c:20225)()

ValueError: Cannot add integral value to Timestamp without offset.
Comments