<div dir="ltr">Hi Paul,<div><br></div><div>Thanks for your response! I did not find a Pandas list for users, only for developers. I'd love to be on there.</div><div><br></div><div><pre style="white-space:pre-wrap">result = a.subtract(b.shift()).dropna()</pre></div><div>This seems verbose, several layers of parenthesis follow by a dot method. I'm new to Python, I thought Python code would be pity and short. Is this what everyone will write?</div><div><br></div><div>Thank you!</div><div><pre style="white-space:pre-wrap"><br></pre></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 13, 2019 at 6:50 PM Paul Hobson <<a href="mailto:pmhobson@gmail.com" target="_blank">pmhobson@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>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-m_844511772299979860gmail-m_-2112474692217377193gmail-output_subarea gmail-m_844511772299979860gmail-m_-2112474692217377193gmail-output_text gmail-m_844511772299979860gmail-m_-2112474692217377193gmail-output_stream gmail-m_844511772299979860gmail-m_-2112474692217377193gmail-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" target="_blank">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_844511772299979860gmail-m_-2112474692217377193gmail-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>
_______________________________________________<br>
SciPy-User mailing list<br>
<a href="mailto:SciPy-User@python.org" target="_blank">SciPy-User@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/scipy-user" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/scipy-user</a><br>
</blockquote></div>