def diagonals(a):
    rows, cols = a.shape
if cols > rows:
    a = a.T
rows, cols = a.shape
fill = numpy.zeros((cols - 1, cols), dtype=a.dtype)
stacked = numpy.vstack((a, fill, a))
major_stride, minor_stride = stacked.strides
strides = major_stride, minor_stride * (cols + 1)
shape = rows + cols - 1, cols
return numpy.lib.stride_tricks.as_strided(stacked, shape, strides)
Find diagonals sums in numpy (faster)