Setting Series as index
<p>I'm using python 2.7 to take a numerical column of my dataframe <code>data</code> and make it an individual object (series) with an index of dates that is another column from <code>data</code>. </p>

<pre><code>new_series = pd.Series(data['numerical_column'] , index=data['dates'])
</code></pre>

<p>However, when I do this, I get a bunch of <code>NaN</code> values in the Series: </p>

<pre><code>dates
1980-01-31   NaN
1980-02-29   NaN
1980-03-31   NaN
1980-04-30   NaN
1980-05-31   NaN
1980-06-30   NaN
...
</code></pre>

<p>Why does my <code>numerical_data</code> values just disappear? </p>

<p>I realize that I can apparently achieve this goal by doing the following, although I'm curious why my initial approach failed. </p>

<pre><code>new_series = data.set_index('dates')['numerical_column']
</code></pre>

<p>I think there is problem with not align index of column <code>data['numerical_column']</code>.</p>

<p>So need convert it to <code>numpy array</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.values.html" rel="nofollow"><code>values</code></a>:</p>

<pre><code>new_series = pd.Series(data['numerical_column'].values , index=data['dates'])
</code></pre>

<p>Sample:</p>

<pre><code>import pandas as pd
import datetime

data = pd.DataFrame({
'dates': {0: datetime.date(1980, 1, 31), 1: datetime.date(1980, 2, 29), 
          2: datetime.date(1980, 3, 31), 3: datetime.date(1980, 4, 30), 
          4: datetime.date(1980, 5, 31), 5: datetime.date(1980, 6, 30)}, 
'numerical_column': {0: 1, 1: 4, 2: 5, 3: 3, 4: 1, 5: 0}})
print (data)
        dates  numerical_column
0  1980-01-31                 1
1  1980-02-29                 4
2  1980-03-31                 5
3  1980-04-30                 3
4  1980-05-31                 1
5  1980-06-30                 0

new_series = pd.Series(data['numerical_column'].values , index=data['dates'])
print (new_series)
dates
1980-01-31    1
1980-02-29    4
1980-03-31    5
1980-04-30    3
1980-05-31    1
1980-06-30    0
dtype: int64
</code></pre>

<p>But method with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_index.html" rel="nofollow"><code>set_index</code></a> is nicer, but slowier:</p>

<pre><code>#[60000 rows x 2 columns]
data = pd.concat([data]*10000).reset_index(drop=True)

In [65]: %timeit pd.Series(data['numerical_column'].values , index=data['dates'])
1000 loops, best of 3: 308 Âµs per loop

In [66]: %timeit data.set_index('dates')['numerical_column']
1000 loops, best of 3: 1.28 ms per loop
</code></pre>

<p><strong>Verification</strong>:</p>

<p>If index of column has same index, it works nice:</p>

<pre><code>s = data.set_index('dates')['numerical_column']
df = s.to_frame()
print (df)
            numerical_column
dates                       
1980-01-31                 1
1980-02-29                 4
1980-03-31                 5
1980-04-30                 3
1980-05-31                 1
1980-06-30                 0

new_series = pd.Series(df['numerical_column'] , index=data['dates'])
print (new_series)
dates
1980-01-31    1
1980-02-29    4
1980-03-31    5
1980-04-30    3
1980-05-31    1
1980-06-30    0
Name: numerical_column, dtype: int64
</code></pre>

python python-2.7 pandas dataframe series