Question

My problem is simple. I have timestamps data from twitter. Each row is a user, each column gives the last time the user twitted.

time_0                  time_1            time_2             time_3     
21/03/2014 16:17    21/03/2014 15:40    21/03/2014 14:55    21/03/2014 12:50         
21/03/2014 16:29    21/03/2014 16:26    21/03/2014 16:23    21/03/2014 16:21    
04/07/2012 13:43    04/07/2012 13:37    04/07/2012 13:34    04/07/2012 13:29        
19/03/2014 01:41    18/03/2014 01:19    17/03/2014 00:50    05/03/2014 22:30    

What I would like to do is getting time differences. For each column, I would like to replace the date and time by the time since the last post happened. For example, if my first post happened at 8 pm, and my second post at 8 45, I want to get '45 minutes' in my first column. Ideally, my output is like this (the difference is calculated in seconds)

time_0  time_1     time_2       time_3
2220    2700       7500         43860
180     180        120            0
360     180        300           300
87720   88140   -4138800       5794500
60        0         0             0
74340   1800        0            540

I do it like this:

df = pandas.read_csv("testtimedelta.csv",header=0,parse_dates=column_names)
df=df.dropna()#get rid of not complete rows

column_names=[]
for i in range(100):
    column_names.append('time_'+str(i))

deltadatas=df[column_names]
for i in range(len(column_names)-1): 
    deltadatas[column_names[i]]=deltadatas[column_names[i]]-deltadatas[column_names[i+1]]/ np.timedelta64(1,'s')

This seems right, except for certain cells it returns a result that has nothing to do with the input, for example 4 million seconds where it should be 1 million. Sometimes it even returns a negative result, as you can see in my output example above.

Is anyone able to explain what happened? Suggest a better way to do it?

I am using numpy version 1.8.0, and pandas version 0.13.0

EDIT: an example of what is wrong.

state   followers   friends tweets_number   time_0                  source_0        time_1                   source_1          time_2                source_2        time_3
Bot     3890        2222        1211        19/03/2014 01:41        twitterfeed     18/03/2014 01:19        twitterfeed     17/03/2014 00:50        twitterfeed     05/03/2014 22:30

In this example, time2-time3 will give me -47 days, which is impossible, and if I do what @Jeff suggested below, again 47 days.

Thanks very much for any help!!

Was it helpful?

Solution

Timedelta docs are here

In [29]: df1 = DataFrame(dict([ ("t{0}".format(i),date_range('20130101 01:0{0}'.format(i*3),periods=5,freq='T')) for i in range(2) ]))

In [30]: df2 = DataFrame(dict([ ("t{0}".format(i+3),date_range('20130101 01:0{0}'.format(i*5),periods=5,freq='T')) for i in range(2) ]))

In [31]: df = df1.join(df2)

In [32]: df
Out[32]: 
                   t0                  t1                  t3                  t4
0 2013-01-01 01:00:00 2013-01-01 01:03:00 2013-01-01 01:00:00 2013-01-01 01:05:00
1 2013-01-01 01:01:00 2013-01-01 01:04:00 2013-01-01 01:01:00 2013-01-01 01:06:00
2 2013-01-01 01:02:00 2013-01-01 01:05:00 2013-01-01 01:02:00 2013-01-01 01:07:00
3 2013-01-01 01:03:00 2013-01-01 01:06:00 2013-01-01 01:03:00 2013-01-01 01:08:00
4 2013-01-01 01:04:00 2013-01-01 01:07:00 2013-01-01 01:04:00 2013-01-01 01:09:00

[5 rows x 4 columns]

In [33]: (df.T-df.T.shift()).T.astype('timedelta64[s]')
Out[33]: 
   t0   t1   t3   t4
0 NaN  180 -180  300
1 NaN  180 -180  300
2 NaN  180 -180  300
3 NaN  180 -180  300
4 NaN  180 -180  300

[5 rows x 4 columns]

IIRC the astype requires pandas 0.13.1 (but you can always df.apply(lambda x: x/np.timedelta64(1,'s'))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top