'Pandas column-wise rolling works with np.float64 but returns empty array with np.float32 and np.float16

I ran into a strange observation where the same code works with np.float64 but not with np.float32 or np.float16. Here's code to reproduce the results:

>>> import numpy as np
>>> import pandas as pd

>>> a = np.array([[1, 2, 3]])
>>> print(pd.DataFrame(a, dtype=np.float64).rolling(3, axis=1).mean())

    0   1    2
0 NaN NaN  2.0

>>> print(pd.DataFrame(a, dtype=np.float32).rolling(3, axis=1).mean())

Empty DataFrame
Columns: []
Index: [0]

np.float16 produces the same result.

This happens on numpy 1.20.3 and pandas 1.2.4. I experimented with numpy 1.20.0 and it worked fine.

I managed to remove errors in my code by explicitly setting the datatype as np.float64, but couldn't figure out why this happens.

Does anyone has any ideas about the root of the problem? And does the problem come from numpy or pandas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source