rename the pandas Series
<p>I have some wire thing when renaming the pandas Series by the datetime.date</p>

<pre><code>import pandas as pd
a = pd.Series([1, 2, 3, 4], name='t')
</code></pre>

<p>I got <code>a</code> is:</p>

<pre><code>0    1
1    2
2    3
3    4
Name: t, dtype: int64
</code></pre>

<p>Then, I have:</p>

<pre><code>ts = pd.Series([pd.Timestamp('2016-05-16'), 
                pd.Timestamp('2016-05-17'), 
                pd.Timestamp('2016-05-18'), 
                pd.Timestamp('2016-05-19')], name='time')
</code></pre>

<p>with <code>ts</code> as:</p>

<pre><code>0   2016-05-16
1   2016-05-17
2   2016-05-18
3   2016-05-19
Name: time, dtype: datetime64[ns]
</code></pre>

<p>Now, if I do:</p>

<pre><code>ts_date = ts.apply(lambda x: x.date())
dates = ts_date.unique()
</code></pre>

<p>I got <code>dates</code> as:</p>

<pre><code>array([datetime.date(2016, 5, 16), datetime.date(2016, 5, 17),
       datetime.date(2016, 5, 18), datetime.date(2016, 5, 19)], dtype=object)
</code></pre>

<hr>

<p>I have two approaches. The wired thing is, if I do the following renaming (approach 1):</p>

<pre><code>for one_date in dates:
    a.rename(one_date)
    print one_date, a.name
</code></pre>

<p>I got:</p>

<pre><code>2016-05-16 t
2016-05-17 t
2016-05-18 t
2016-05-19 t
</code></pre>

<p>But if I do it like this (approach 2):</p>

<pre><code>for one_date in dates:
    a = pd.Series(a, name=one_date)
    print one_date, a.name

2016-05-16 2016-05-16
2016-05-17 2016-05-17
2016-05-18 2016-05-18
2016-05-19 2016-05-19
</code></pre>

<hr>

<p>My question is: why the method <code>rename</code> does not work (in approach 1)?</p>

<p>Because <code>rename</code> does not change the object unless you set the <code>inplace</code> argument as <code>True</code>, as seen in the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.rename.html" rel="nofollow">docs</a>.</p>

<p>Notice that the <code>copy</code> argument can be used so you don't have to create a new series passing the old series as argument, like in your second example.</p>

python python-2.7 pandas