Skip to content

BUG: Series.std and Series.var give incorrect results for complex values. #61645

Closed
@randolf-scholz

Description

@randolf-scholz
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

    I have confirmed this bug exists on the latest version of pandas.

    I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd

arr = np.array([-1j, 0j, 1j], dtype=complex)
s = pd.Series(arr, dtype=complex)

print(arr.std(ddof=0))  # 0.816496580927726
print(s.std(ddof=0))  # nan
print(arr.var(ddof=0))  # 0.666
print(s.var(ddof=0))  # -0.666

Issue Description

  1. The results diverge from numpy.
  2. pandas yields nonsensical results like negative floats.

Expected Behavior

For complex variables, std and var should give non-negative floating results. Recall that σ 𝔼 | x μ | 2 . Often, authors that only use real-valued variables leave out the absolute value, which I guess is what happened here.

Installed Versions

INSTALLED VERSIONS

commit : 2cc3762
python : 3.13.4
python-bits : 64
OS : Linux
OS-release : 6.11.0-26-generic
Version : #26~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr 17 19:20:47 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.0
numpy : 2.3.0
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.1.1
Cython : None
sphinx : 8.2.3
IPython : 9.3.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.5.1
html5lib : None
hypothesis : 6.135.9
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.3
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 20.0.0
pyreadstat : None
pytest : 8.4.0
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @randolf-scholz

      Issue actions

        BUG: `Series.std` and `Series.var` give incorrect results for complex values. · Issue #61645 · pandas-dev/pandas