pandas date to string
<p>i have a datetime <code>pandas.Series</code>. One column called "dates". I want to get 'i' element in loop like string.</p>

<p><code>s.apply(lambda x: x.strftime('%Y.%m.%d'))</code> or
<code>astype(str).tail(1).reset_index()['date']</code> or many other solutions don't work.</p>

<p>I just want a string like <code>'2016-09-16'</code> (first datetime element in series) and not what is currently returned, which is:</p>

<pre><code> ss = series_of_dates.astype(str).tail(1).reset_index()['date']
"lol = %s" % ss 
</code></pre>

<blockquote>
  <p><code>lol = 0    2016-09-16\nName: date, dtype: object</code></p>
</blockquote>

<p>I need just:</p>

<blockquote>
  <p><code>lol = 2016-09-16</code></p>
</blockquote>

<p>because I need </p>

<blockquote>
  <p><strong><code>some string</code></strong> % a , b , s ,d</p>
</blockquote>

<p>.....  without even '/n' in a, b ,s ...</p>

<p>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.strftime.html" rel="nofollow"><code>strftime</code></a> for convert <code>datetime</code> column to <code>string</code> column:</p>

<pre><code>import pandas as pd

start = pd.to_datetime('2015-02-24 10:00')
rng = pd.date_range(start, periods=10)

df = pd.DataFrame({'dates': rng, 'a': range(10)})  
print (df)
   a               dates
0  0 2015-02-24 10:00:00
1  1 2015-02-25 10:00:00
2  2 2015-02-26 10:00:00
3  3 2015-02-27 10:00:00
4  4 2015-02-28 10:00:00
5  5 2015-03-01 10:00:00
6  6 2015-03-02 10:00:00
7  7 2015-03-03 10:00:00
8  8 2015-03-04 10:00:00
9  9 2015-03-05 10:00:00

s = df.dates
print (s.dt.strftime('%Y.%m.%d'))
0    2015.02.24
1    2015.02.25
2    2015.02.26
3    2015.02.27
4    2015.02.28
5    2015.03.01
6    2015.03.02
7    2015.03.03
8    2015.03.04
9    2015.03.05
Name: dates, dtype: object
</code></pre>

<p>Loop with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.iteritems.html" rel="nofollow"><code>Series.iteritems</code></a>:</p>

<pre><code>for idx, val in s.dt.strftime('%Y.%m.%d').iteritems():
    print (val)

    2015.02.24
2015.02.25
2015.02.26
2015.02.27
2015.02.28
2015.03.01
2015.03.02
2015.03.03
2015.03.04
2015.03.05
</code></pre>

python datetime pandas time-series