<h2 dir="auto">Feature</h2>
<p dir="auto">As the subject says it will be great if <code class="notranslate">np.fill_diagonal</code> had a k-ith diagonal argument as <code class="notranslate">np.diag</code> does. The behavior expected (and a hack for the solution) is better explained in the <a href="https://stackoverflow.com/questions/65299295/assign-shifted-diagonal-values-with-np-diag/65299483" rel="nofollow">following StackOverflow question</a> by me</p>
<h2 dir="auto">Expected behaviour</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; u = np.zeros(25).reshape(5, 5)
&gt;&gt;&gt; u
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
&gt;&gt;&gt; np.fill_diagonal(u, 1, k=1)
&gt;&gt;&gt; u
array([[0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0.]])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-c1">25</span>).<span class="pl-en">reshape</span>(<span class="pl-c1">5</span>, <span class="pl-c1">5</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">fill_diagonal</span>(<span class="pl-s1">u</span>, <span class="pl-c1">1</span>, <span class="pl-s1">k</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>]])</pre></div>
<h2 dir="auto">Current solution</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; u = np.zeros(25).reshape(5, 5)
&gt;&gt;&gt; u
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
&gt;&gt;&gt; u[np.eye(len(u), k=1, dtype='bool')] = 1
&gt;&gt;&gt; u
array([[0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0.]])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">zeros</span>(<span class="pl-c1">25</span>).<span class="pl-en">reshape</span>(<span class="pl-c1">5</span>, <span class="pl-c1">5</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>[<span class="pl-s1">np</span>.<span class="pl-en">eye</span>(<span class="pl-en">len</span>(<span class="pl-s1">u</span>), <span class="pl-s1">k</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">'bool'</span>)] <span class="pl-c1">=</span> <span class="pl-c1">1</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>]])</pre></div>
<h2 dir="auto">Why this syntax?</h2>
<p dir="auto">This is already intuitive as <code class="notranslate">np.diag</code> use the same syntax. For example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; u
array([[0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.],
       [0., 0., 0., 0., 0.]])
&gt;&gt;&gt; np.diag(u, k=1)
array([1., 1., 1., 1.])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">u</span>
<span class="pl-en">array</span>([[<span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">0.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">1.</span>],
       [<span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>, <span class="pl-c1">0.</span>]])
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">diag</span>(<span class="pl-s1">u</span>, <span class="pl-s1">k</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)
<span class="pl-en">array</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>, <span class="pl-c1">1.</span>])</pre></div>
<p dir="auto">Unfortunately, as <code class="notranslate">np.diag</code> is a function, one cannot assign a value like <code class="notranslate">np.diag(u, k=1) = 1</code>. That's why implementing the <code class="notranslate">k</code> argument on <code class="notranslate">np.fill_diagonal</code> should be desired.</p>
<p dir="auto">I want to take this issue and make a PR. Just want to discuss it before to better implement it and document it.</p>
<p dir="auto">It would be really nice if <code class="notranslate">np.fill_diagonal</code> could fill other diagonals besides the main diagonal.  This would match the <code class="notranslate">offset</code> argument of <code class="notranslate">np.diagonal.</code></p>
<p dir="auto">Shouldn't require much code to allow this.</p>