Skip to content

Commit 1da1279

Browse files
bunardshengHappyHorse
authored and
im-vinicius
committed
BUG: Fix incompatible return value type in pandas/core/generic.py (pandas-dev#52897)
Co-authored-by: HappyHorse <[email protected]>
1 parent de68caa commit 1da1279

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,12 @@ Numeric
332332
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
333333
- Bug in :meth:`Series.mean`, :meth:`DataFrame.mean` with object-dtype values containing strings that can be converted to numbers (e.g. "2") returning incorrect numeric results; these now raise ``TypeError`` (:issue:`36703`, :issue:`44008`)
334334
- Bug in :meth:`DataFrame.corrwith` raising ``NotImplementedError`` for pyarrow-backed dtypes (:issue:`52314`)
335+
- Bug in :meth:`DataFrame.size` and :meth:`Series.size` returning 64-bit integer instead of int (:issue:`52897`)
335336
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
336337
- Bug in :meth:`Series.median` and :meth:`DataFrame.median` with object-dtype values containing strings that can be converted to numbers (e.g. "2") returning incorrect numeric results; these now raise ``TypeError`` (:issue:`34671`)
337338
-
338339

340+
339341
Conversion
340342
^^^^^^^^^^
341343
- Bug in :func:`DataFrame.style.to_latex` and :func:`DataFrame.style.to_html` if the DataFrame contains integers with more digits than can be represented by floating point double precision (:issue:`52272`)

pandas/core/generic.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,8 @@ def size(self) -> int:
657657
>>> df.size
658658
4
659659
"""
660-
# error: Incompatible return value type (got "signedinteger[_64Bit]",
661-
# expected "int") [return-value]
662-
return np.prod(self.shape) # type: ignore[return-value]
660+
661+
return int(np.prod(self.shape))
663662

664663
def set_axis(
665664
self,
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame
5+
6+
7+
@pytest.mark.parametrize(
8+
"data, index, expected",
9+
[
10+
({"col1": [1], "col2": [3]}, None, 2),
11+
({}, None, 0),
12+
({"col1": [1, np.nan], "col2": [3, 4]}, None, 4),
13+
({"col1": [1, 2], "col2": [3, 4]}, [["a", "b"], [1, 2]], 4),
14+
({"col1": [1, 2, 3, 4], "col2": [3, 4, 5, 6]}, ["x", "y", "a", "b"], 8),
15+
],
16+
)
17+
def test_size(data, index, expected):
18+
# GH#52897
19+
df = DataFrame(data, index=index)
20+
assert df.size == expected
21+
assert isinstance(df.size, int)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
from pandas import Series
4+
5+
6+
@pytest.mark.parametrize(
7+
"data, index, expected",
8+
[
9+
([1, 2, 3], None, 3),
10+
({"a": 1, "b": 2, "c": 3}, None, 3),
11+
([1, 2, 3], ["x", "y", "z"], 3),
12+
([1, 2, 3, 4, 5], ["x", "y", "z", "w", "n"], 5),
13+
([1, 2, 3], None, 3),
14+
([1, 2, 3], ["x", "y", "z"], 3),
15+
([1, 2, 3, 4], ["x", "y", "z", "w"], 4),
16+
],
17+
)
18+
def test_series(data, index, expected):
19+
# GH#52897
20+
ser = Series(data, index=index)
21+
assert ser.size == expected
22+
assert isinstance(ser.size, int)

0 commit comments

Comments
 (0)