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>You use <code>np.subtract</code> directly:</p>

<pre><code>np.subtract(d2, d1)
</code></pre>

<p>Which'll give you a TimedeltaIndex as a result:</p>

<pre><code>TimedeltaIndex(['366 days', '366 days'], dtype='timedelta64[ns]', freq=None)
</code></pre>

<p>Then if wanted use <code>.days</code> on that.</p>

<p>Another possible way:</p>

<pre><code>pd.to_timedelta(d2.values - d1.values).days
</code></pre>

<p>Which'll leave you with:</p>

<pre><code>array([366, 366])
</code></pre>

python pandas