Margherita Di Leo
Margherita Di Leo

Reputation: 83

create a time series picking the values from an existing one but changing the dates in python

I have a time series and want to duplicate it and assign a new set of date index to it, because I need to concatenate it to the original series (I need a longer time series in order to perform filtering).

>>>len(ts_interp)
109
>>>ts_interp.head()
date
2016-12-06    0.118412
2016-12-13    0.741708
2016-12-20    0.729774
2016-12-27    0.717839
2017-01-03    0.705905
Freq: 7D, Name: 2, dtype: float64
>>>ts.interp.tail()
date
2018-12-04    0.022732
2018-12-11    0.022732
2018-12-18    0.022732
2018-12-25    0.022732
2019-01-01    0.022732
Freq: 7D, Name: 2, dtype: float64
>>>t1 = pd.to_datetime('2016-12-06') #first item
>>>t2 = pd.to_datetime('2019-01-01') #last item
>>>t2-t1
Timedelta('756 days 00:00:00')
# the last item of my new time series to be concatenated, 
# should be the nearest to:
>>>t1 - dt.timedelta(7)
Timestamp('2016-11-29 00:00:00')

My time series must respect seasonality, so the value that I want to pick as last value of the new time series should be the nearest to 201x-11-29, namely 2018-11-27 (the year must become 2016). I proceed like this:

>>>pre_values=ts_interp.loc['2016-12-06':'2018-11-27']
>>>pre_index = pd.date_range(start='2014-12-06', end='2016-11-27', freq='7D')
>>>pre_series=pd.Series(pre_values, index=pre_index)

But my pre_series contains only NaN.

2014-12-06   NaN
2014-12-13   NaN
2014-12-20   NaN
2014-12-27   NaN
2015-01-03   NaN
...

How can I create a time series picking the values from the existing one but changing the dates? Thanks in advance

Upvotes: 0

Views: 50

Answers (1)

Inon Peled
Inon Peled

Reputation: 711

I don't understand what you are trying to accomplish by concatenating a time series twice for "filtering", but if you insist, try to conclude with:

pre_series.loc[start_date1:end_date1] = \
    pre_series.loc[start_date2:end_date2].values

Namely assign the values of the lower half to the upper half.

Upvotes: 1

Related Questions