'Pandas rolling removes imaginary part of complex... defect or feature?

Pandas rolling appears only to return the real part of complex values...

Am I missing something?

a=np.array([[1+0j,2+0j,3+0j],[4j,5j,10+6j]])
pd.DataFrame(a.T).rolling(3).apply(lambda x: print(x) is None)

Output:

0   1
1   2
2   3
dtype: float64
0    0
1    0
2   10
dtype: float64
    0   1
0   NaN NaN
1   NaN NaN
2   1   1

Note that the problem is not with apply. When apply is used without rolling, the entire complex value is passed to the lambda function:

a=np.array([[1+0j,2+0j,3+0j],[4j,5j,10+6j]])
pd.DataFrame(a.T).apply(lambda x: print(x) is None)

Output:

0   1+0j
1   2+0j
2   3+0j
Name: 0, dtype: complex128
0    0+4j
1    0+5j
2   10+6j
Name: 1, dtype: complex128
0    True
1    True
dtype: bool

** Note: this is not a question of whether or not to use rolling - but rather about the behavior of rolling with complex values.



Solution 1:[1]

I just changed the mentioned line in the warning ensure_float64 because it checks the values to be float64, as follows:

values = values

Then changed the dtype as it produces buffer error, as follows:

import pandas as pd
import numpy as np

a=np.array([[1+0j,2+0j,3+0j],[4j,5j,10+6j]])
pd.DataFrame(a.T).astype(np.complex64).rolling(3).apply(lambda x: print(x) is None)

The result:

0    1.0+0.0j
1    2.0+0.0j
2    3.0+0.0j
dtype: complex64
0     0.0+4.0j
1     0.0+5.0j
2    10.0+6.0j
dtype: complex64
0   1
0   NaN NaN
1   NaN NaN
2   1.0 1.0

rolling file location:

C:\Users\user\anaconda3\pkgs\pandas-1.2.4-py38hd77b12b_0\Lib\site-packages\pandas\core\window

line to edit: 348.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1