EDIT: This question was asked in 2016 and similar questions have been posted on SO years later after the functionality was finally removed, e.g. module 'pandas' has
If your dimensions are homogeneous, you could try to implement an n-dimensional form of the Summed Area Table used for bidimensional images:
A summed area table is a data structure and algorithm for quickly and efficiently generating the sum of values in a rectangular subset of a grid.
Then, in this order, you could:
Unfortunately I cannot know if this is efficient or not, but the by the given premise, it should be.