Description
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 pandas as pd
pdf = pd.DataFrame(
{
"A": [1, -2, 3, -4, 5] * 300,
"B": [1.0, -2, 3, -4, 5] * 300,
"C": [-6.0, -7, -8, -9, 10] * 300,
"D": [True, False, True, False, False] * 300,
},
index=range(10, 15001, 10),
)
pdf.var(axis=1)
Issue Description
DataFrame.var()
with axis=1
returns float64
Series in Pandas 1.5.3, but now it returns object
Series instead. So I wonder if it is an intended behavior or an unexpected bug:
Pandas 1.5.3
>>> pdf = pd.DataFrame(
... {
... "A": [1, -2, 3, -4, 5] * 300,
... "B": [1.0, -2, 3, -4, 5] * 300,
... "C": [-6.0, -7, -8, -9, 10] * 300,
... "D": [True, False, True, False, False] * 300,
... },
... index=range(10, 15001, 10),
... )
>>> pdf.var(axis=1)
10 12.250000
20 8.916667
30 27.583333
40 13.583333
50 16.666667
...
14960 12.250000
14970 8.916667
14980 27.583333
14990 13.583333
15000 16.666667
Length: 1500, dtype: float64
Pandas 2.0.0 and above
>>> pdf = pd.DataFrame(
... {
... "A": [1, -2, 3, -4, 5] * 300,
... "B": [1.0, -2, 3, -4, 5] * 300,
... "C": [-6.0, -7, -8, -9, 10] * 300,
... "D": [True, False, True, False, False] * 300,
... },
... index=range(10, 15001, 10),
... )
>>> pdf.var(axis=1)
10 12.25
20 8.916667
30 27.583333
40 13.583333
50 16.666667
...
14960 12.25
14970 8.916667
14980 27.583333
14990 13.583333
15000 16.666667
Length: 1500, dtype: object
Expected Behavior
>>> pdf = pd.DataFrame(
... {
... "A": [1, -2, 3, -4, 5] * 300,
... "B": [1.0, -2, 3, -4, 5] * 300,
... "C": [-6.0, -7, -8, -9, 10] * 300,
... "D": [True, False, True, False, False] * 300,
... },
... index=range(10, 15001, 10),
... )
>>> pdf.var(axis=1)
10 12.250000
20 8.916667
30 27.583333
40 13.583333
50 16.666667
...
14960 12.250000
14970 8.916667
14980 27.583333
14990 13.583333
15000 16.666667
Length: 1500, dtype: float64
Installed Versions
INSTALLED VERSIONS
commit : ba1cccd
python : 3.9.17.final.0
python-bits : 64
OS : Darwin
OS-release : 22.6.0
Version : Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:53 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8
pandas : 2.1.0
numpy : 1.26.0
pytz : 2023.3
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 8.15.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.2
sqlalchemy : 2.0.20
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None