Pyspark adding few seconds to time
<p>I am trying to add few seconds to time but I haven't been successful. Here is my example</p>

<pre><code>import datetime
str1 = sc.parallelize(["170745","140840"])
aa = str1.map(lambda l: datetime.datetime.strptime(l, '%H%M%S').strftime('%H:%M:%S'))
</code></pre>

<p>yields</p>

<pre><code>['17:07:45', '14:08:40'] 
</code></pre>

<p>but I want is</p>

<pre><code>['17:07:52', '14:08:47']
</code></pre>

<p>How could I add 7 seconds to each converted time. I know <code>timedelta</code> is there but not sure about that.</p>

<p>You can add <code>datetime.timedelta(0,7)</code> after you have convert your string to dates:</p>

<pre><code>import datetime
str1 = sc.parallelize(["170745","140840"])
aa = str1.map(lambda l: (datetime.datetime.strptime(l, '%H%M%S') + datetime.timedelta(0,7)).strftime('%H:%M:%S'))
</code></pre>

<p><code>aa.collect()</code> returns:</p>

<pre><code>['17:07:52', '14:08:47']
</code></pre>

<p>Replacing the lambda with a regular function, arguably makes it easier to understand:</p>

<pre><code>import datetime

def processdate(timeString):
    date = datetime.datetime.strptime(timeString, '%H%M%S')
    date += datetime.timedelta(0,7)
    return date.strftime('%H:%M:%S')    

str1 = sc.parallelize(["170745","140840"])    
aa = str1.map(processdate)
</code></pre>

python datetime pyspark