<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>This is more a question for the pandas list, but since i'm here i'll take a crack.</div><div><br></div><ul><li>numpy aligns arrays by position. </li><li>pandas aligns by label.</li></ul><div>So what you did in pandas is roughly equivalent to the following:</div><div><br></div><div><span style="font-family:monospace,monospace">a = pandas.Series([85, 86, 87, 86], name='a').iloc[1:4].to_frame()<br>b = pandas.Series([15, 72, 2, 3], name='b').iloc[0:3].to_frame()<br>result = a.join(b,how='outer').assign(diff=lambda df: df['a'] - df['b'])</span></div><div><span style="font-family:monospace,monospace">print(result)</span></div><div>
<div class="gmail-output_subarea gmail-output_text gmail-output_stream gmail-output_stdout"><pre> a b diff
0 NaN 15.0 NaN
1 86.0 72.0 14.0
2 87.0 2.0 85.0
3 86.0 NaN NaN<br><br>
<span style="font-family:arial,helvetica,sans-serif">So what I think you want would be the following:</span>
</pre><pre>a = pandas.Series([85, 86, 87, 86], name='a')<br>b = pandas.Series([15, 72, 2, 3], name='b')<br>result = a.subtract(b.shift()).dropna()<br>print(result)<br>1 71.0
2 15.0
3 84.0
dtype: float64
</pre></div>
</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 13, 2019 at 2:51 PM C W <<a href="mailto:tmrsg11@gmail.com">tmrsg11@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Dear list,<div><br></div><div>I have the following to Pandas Series: a, b. I want to slice and then subtract. Like this: a[1:4] - b[0:3]. Why does it give me NaN? But it works in Numpy.</div><div><div style="line-height:23px"><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:15px;white-space:pre-wrap"><br></div><div style="white-space:pre-wrap">Example 1: did not work</div><div style="color:rgb(0,0,0);font-family:Menlo,Monaco,"Courier New",monospace;font-size:15px;white-space:pre-wrap"><span style="font-family:Arial,Helvetica,sans-serif;font-size:small;color:rgb(34,34,34)">>>>a = pd.Series([85, 86, 87, 86])</span><br></div></div><div style="line-height:23px">>>>b = pd.Series([15, 72, 2, 3])<div>>>> a[1:4]-b[0:3]<font face="Menlo, Monaco, Courier New, monospace" color="#000000"><span style="font-size:15px;white-space:pre-wrap">
</span></font>0 NaN<font face="Menlo, Monaco, Courier New, monospace" color="#000000"><span style="font-size:15px;white-space:pre-wrap">
</span></font>1 14.0<font face="Menlo, Monaco, Courier New, monospace" color="#000000"><span style="font-size:15px;white-space:pre-wrap">
</span></font>2 85.0<font face="Menlo, Monaco, Courier New, monospace" color="#000000"><span style="font-size:15px;white-space:pre-wrap">
</span></font>3 NaN<br></div><div><div>>>> type(a[1:4])</div><div><class 'pandas.core.series.Series'></div><div><br></div></div><div>Example 2: worked</div></div></div><div style="line-height:23px">If I use values() method, it's converted to a Numpy object. And it works!</div><div style="line-height:23px"><div style="line-height:23px">>>> a.values[1:4]-b.values[0:3]</div><div style="line-height:23px">array([71, 15, 84])</div><div style="line-height:23px"><div>>>> type(a.values[1:4])</div><div><class 'numpy.ndarray'></div><br class="gmail-m_-8744438713277675593gmail-Apple-interchange-newline"></div><div style="line-height:23px">What's the reason that Pandas in example 1 did not work? Isn't Numpy built on top of Pandas? So, why is everything ok in Numpy, but not in Pandas?</div><div style="line-height:23px"><br></div><div style="line-height:23px">Thanks in advance!</div></div></div></div></div></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>