Datetime issues while time series predicting in Pa

2019-09-18 06:55发布

Trying to implement the model of time series predicting in python but facing with issues with datetime data.

So I have a dataframe 'df' with two columns of datetime and float types:

enter image description here

Then I try to build an array using values method. But smth strange happens and it displays the date in strange format with timestamps and time:

enter image description here

And basically because of it, I can not implement the model receiving the following messages for example:"Cannot add integral value to Timestamp without freq."

So what seems to be the problem and how can it be solved?

2条回答
劳资没心,怎么记你
2楼-- · 2019-09-18 07:41

you can convert your integer into a timedelta, and do the calculations as you did before:

from datetime import timedelta

interval = timedelta(days = 5)

#5 days later
time_stamp += interval
查看更多
再贱就再见
3楼-- · 2019-09-18 07:48

It's complicated.

First of all, when creating a numpy array, all types will be the same. However, datetime64 is not the same as int. So we'll have to resolve that, and we will.

Second, you tried to do this with df.values. Which makes sense, however, what happens is that pandas makes the whole df into dtype=object then into an object array. The problem with that is that Timestamps get left as Timestamps which is getting in your way.

So I'd convert them on my own like this

a = np.column_stack([df[c].values.astype(int) for c in ['transaction_date', 'amount']])

a

array([[1454284800000000000,                   1],
       [1454371200000000000,                   2],
       [1454457600000000000,                   3],
       [1454544000000000000,                   4],
       [1454630400000000000,                   5]])

We can always convert the first column of a back like this

a[:, 0].astype(df.transaction_date.values.dtype)

array(['2016-02-01T00:00:00.000000000', '2016-02-02T00:00:00.000000000',
       '2016-02-03T00:00:00.000000000', '2016-02-04T00:00:00.000000000',
       '2016-02-05T00:00:00.000000000'], dtype='datetime64[ns]')
查看更多
登录 后发表回答