向时间序列数据追加日期时出错



我正在尝试将日期附加到时间序列数据。

以下是时间序列数据的输出,命名为timseries_data:

timseries_data
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-11-01 00:00:00, ..., 2014-02-15 00:00:00]
Length: 107, Freq: D, Timezone: None

我想用20140216日期追加到日期序列。目前我正在使用追加命令来更新系列。代码:

start_date=datetime.strptime(date_range[1],'%Y%m%d')
date.append(start_date)

但是使用这种方法,我遇到了这个错误:

ValueError: all the input arrays must have same number of dimensions

有人可以建议我任何好的方法来附加日期到时间序列数据?

通常最好避免添加(通常使用concat或join更可取/更有效)。

说,你可以这样做,你只需要确保你附加了一个datetime64(否则你会看到你所看到的错误!):

In [11]: rng = pd.date_range('2014', periods=4)
In [12]: t
Out[12]: Timestamp('2014-03-26 00:49:33.160282', tz=None)
In [13]: t.asm8
Out[13]: numpy.datetime64('2014-03-25T17:49:33.160282000-0700')

加上this将不会显示错误:

In [14]: np.append(a, t.asm8)
Out[14]: 
array(['2014-03-25T17:00:00.000000000-0700',
       '2014-03-26T17:00:00.000000000-0700',
       '2014-03-27T17:00:00.000000000-0700',
       '2014-03-28T17:00:00.000000000-0700',
       '2014-03-25T17:49:33.160282000-0700'], dtype='datetime64[ns]')
In [15]: pd.DatetimeIndex(np.append(a, t.asm8))
Out[15]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-03-26 00:00:00, ..., 2014-03-26 00:49:33.160282]
Length: 5, Freq: None, Timezone: None

*这可能需要最新版本的numpy(即1.7+)。

Try union:

    timeseries_data = pd.date_range('2013-11-01', '2014-02-15', freq='D')
    timeseries_data = timeseries_data.union(pd.date_range('2014-02-16', periods=1))

最新更新