[SciPy-user] calculations using the datetime information of timeseries

Tim Michelsen timmichelsen@gmx-topmail...
Wed Nov 12 17:38:29 CST 2008

```Hello,
I need to perform calculations for a time series that use the datetime
of each data point as input. An example:

def myfunction(datetime_obj, scaling_factor):
pass

I found out that I can get the datetime for each entry with

for i in range(0, series.size):
series[i] =  myfunction(series.dates.tolist()[i], 10.)

Now, I noticed a strange thing.

If I have a base series "base_series" and assige it to a new one with

new_series = base_series

The base_series gets updated/changed according to all calculations I
perform on new_series (Please see method 1 below).

The only way I could imagine to make my code work is createding lots of
template series like in method 3 below. This way lets me calculate my
new values in new_series using the datetime information and still
retrain base_series with its original values.

I kindly ask you to shed some light why the base_series get changed when
I change derived series.

Is there a more efficient way to acomplish my task that I may haven't
thought of so far?

Kind regards,
Timmie

#### BELOW A SAMPLE SCRIPT THAT MAY ILLUSTRATE ####

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
import scikits.timeseries as ts

import numpy as np

#create dummy series
data = np.zeros(600)+1
now = datetime.datetime.now()
start = datetime.datetime(now.year, now.month, now.day)
#print start
start_date = ts.Date('H', datetime=start)
#print start_date
series_dummy = ts.time_series(data, dtype=np.float_, freq='H',
start_date=start_date)

snew = series_dummy

###method 1

for i in range(0,snew.size):
snew[i] = snew[i]* 2 #snew.dates[i].datetime

print "method 1:", snew.sum()-series_dummy.sum()

###method 2

for i in range(0,snew.size):
snew = snew*2

print "method 2:", snew.sum()-series_dummy.sum()

#method 3:

data = np.zeros(series_dummy.size)+1
dt_arr = series_dummy.dates
cser = ts.time_series(data.astype(np.float_), dt_arr)
for i in range(0,cser.size):
#        note: cser.dates[i].datetime.hour is just used as an example
#        my function performes calculations based on the value of the
datetime of each data point for each data point (current datetime is the
input parameter).

cser[i] = cser.dates[i].datetime.hour

print "method 3:", cser.sum()-series_dummy.sum()

```