3

I am trying to append a date to a timeseries data.

Following is the output of the timeseries data, named as 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

I want to append to date series with 20140216 date. Currently I am using append command to update series. Code:

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

But with this approach I am hitting this error :

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

Can somebody suggest me any good ways to append date to timeseries data?

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
user68101
  • 51
  • 3
  • Yeah, `append` still not working in 0.18.1. `.insert` works, but with `insert`, you can't insert at the last place – jf328 Sep 27 '16 at 15:57

2 Answers2

1

Appending is usually best avoided (it's usually preferable/more efficient to use concat or join).

Saying that, you can do it, you just have to ensure you're appending a datetime64 (otherwise you'll see the error which you're seeing!):

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

Appending with this won't show the error:

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

* This may require a recent version of numpy (i.e. 1.7+).

Community
  • 1
  • 1
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • np.append seems to be working. But on your suggestion, I tried using np.concatenate and it didnt work :( – user68101 Mar 26 '14 at 16:26
  • @user68101 suggestion is for DataFrames or Series, pd.concat or join/merges: http://pandas.pydata.org/pandas-docs/stable/merging.html. It really depends what you're doing. – Andy Hayden Mar 26 '14 at 16:36
0

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))
Oink
  • 1