Natural language date differences in python
<p>I have a dynamically generated pandas dataframe with this structure:</p>

<pre><code>name,Events,Last,Elapsed
10.0.0.103,11230,2016-10-11 23:16:45,0 days 00:00:08.708000000
10.0.0.24,14088,2016-10-11 23:16:52,0 days 00:00:01.708000000
</code></pre>

<p>This details the number of events per IP address (name), when the last event was (Last), and how much time has elapsed since that event (Elapsed). The Elapsed column is generated with datetime using the following code:</p>

<pre><code>dfTotalS['Elapsed'] = datetime.datetime.now() - dfTotalS['Last']
</code></pre>

<p>I need the Elapsed column to be in 'natural language', for example:</p>

<pre><code>0 days 00:00:01.708000000 =&gt; 'less than 5 seconds ago'
3 days 00:02:22.708000000 =&gt; 'over 3 days ago'
</code></pre>

<p>I have played around with dateutil without much success. What is the best way of going about this?</p>

<p>Thanks to @Boud for getting me started.</p>

<p>CSV:</p>

<pre><code>name,Events,Last,Elapsed
10.0.0.103,11230,2016-10-11 23:16:45,0 days 00:00:08.708000000
10.0.0.24,14088,2016-10-11 23:16:52,0 days 00:00:01.708000000
</code></pre>

<p>Using texttime.py (<a href="http://code.activestate.com/recipes/498062-nicely-readable-timedelt" rel="nofollow">http://code.activestate.com/recipes/498062-nicely-readable-timedelt</a>ââa)</p>

<pre><code>import texttime

dfTotal['Elapsed'] = dfTotal['Elapsed'].apply(lambda x: texttime.stringify(x)) + ' ago'
print dfTotal
</code></pre>

<p>returns</p>

<pre><code>name,Events,Last,Elapsed
10.0.0.103,11230,2016-10-11 23:16:45,8 seconds ago
10.0.0.24,14088,2016-10-11 23:16:52,one second ago
</code></pre>

python pandas python-datetime