pandas elementwise difference between two DatetimeIndex
<p>Is there a pandas idiomatic way to find the difference in days between two pandas DatetimeIndex?</p>

<pre><code>&gt;&gt;&gt; d1 = pd.to_datetime(['2000-01-01', '2000-01-02'])
&gt;&gt;&gt; d2 = pd.to_datetime(['2001-01-01', '2001-01-02'])
</code></pre>

<p><code>-</code> operator is set difference, ie dates in d1 but not in d2. </p>

<pre><code>&gt;&gt;&gt; d1-d2
DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq=None)
</code></pre>

<p>IMO, this is not consistent with numpy and pure python behaviour. Not even pandas itself</p>

<pre><code>&gt;&gt;&gt; d2[0]-d1[0]
Timedelta('366 days 00:00:00')
</code></pre>

<p>This is what I want, but ugly.</p>

<pre><code>&gt;&gt;&gt; [d.days for d in d2.to_pydatetime() - d1.to_pydatetime()]
[366, 366]
</code></pre>

<p>This is because the <code>dtype</code> is <code>datetimeIndex</code> so arithmetic operations are set-wise by design, if you construct a <code>Series</code> from them then you can perform the element-wise subtraction as desired:</p>

<pre><code>In [349]:
d1 = pd.to_datetime(['2000-01-01', '2000-01-02'])
d2 = pd.to_datetime(['2001-01-01', '2001-01-02'])
s1 = pd.Series(d1)
s2 = pd.Series(d2)
(s1-s2).abs()

Out[349]:
0   366 days
1   366 days
dtype: timedelta64[ns]
</code></pre>

python pandas