Updating Each Row with Minutes Since First Row
<p>I have a file with a million tweets.  The first tweet occurred <code>2013-04-15 20:17:18 UTC</code>. I want to update each tweet row afterward with the minutes since <code>minsSince</code> that first tweet.  </p>

<p>I have found help with datetime <a href="http://stackoverflow.com/questions/2788871/python-date-difference-in-minutes">here</a>, and converting time <a href="http://stackoverflow.com/questions/7703865/going-from-twitter-date-to-python-datetime-date">here</a>, but when I put the two together I don't get the right times. It could be something with the UTC string at the end of each <code>published_at</code> value.</p>

<p>The error it throws is:</p>

<pre><code>tweets['minsSince'] = tweets.apply(timesince,axis=1)
...
TypeError: ('string indices must be integers, not str', u'occurred at index 0')
</code></pre>

<p>Thanks for any help.</p>

<pre><code>#Import stuff
from datetime import datetime
import time
import pandas as pd
from pandas import DataFrame

#Read the csv file
tweets = pd.read_csv('BostonTWEETS.csv')
tweets.head()

#The first tweet's published_at time
starttime = datetime (2013, 04, 15, 20, 17, 18)

#Run through the document and calculate the minutes since the first tweet
def timesince(row):
    minsSince = int()
    tweetTime = row['published_at']
    ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweetTime['published_at'], '%Y-%m-%d %H:%M:%S %UTC'))
    timediff = (tweetTime - starttime)
    minsSince.append("timediff")
    return ",".join(minsSince)

tweets['minsSince'] = tweets.apply(timesince,axis=1)

df = DataFrame(tweets)

print(df)
</code></pre>

<p>Sample csv <a href="https://drive.google.com/open?id=0B1HTWM4qq42VejJfcVhBYTRLUG8" rel="nofollow">file</a> of first 5 rows. </p>

<pre><code>#Import stuff
from datetime import datetime
import time
import pandas as pd
from pandas import DataFrame

#Read the csv file
tweets = pd.read_csv('sample.csv')
tweets.head()

#The first tweet's published_at time
starttime = tweets.published_at.values[0]
starttime = datetime.strptime(starttime, '%Y-%m-%d %H:%M:%S UTC')

#Run through the document and calculate the minutes since the first tweet
def timesince(row):
    ts = datetime.strptime(row, '%Y-%m-%d %H:%M:%S UTC')
    timediff = (ts- starttime)
    timediff = divmod(timediff.days * 86400 + timediff.seconds, 60)
    return timediff[0]

tweets['minSince'] = 0
tweets.minSince = tweets.published_at.map(timesince)

df = DataFrame(tweets)

print(df)
</code></pre>

<p>I hope this is what you are looking for. </p>

python datetime twitter time