Skip to content

BUG: Fixes Incompatible return value type in pandas/core/generic.py #52897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,12 @@ Numeric
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
- 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`)
- Bug in :meth:`DataFrame.corrwith` raising ``NotImplementedError`` for pyarrow-backed dtypes (:issue:`52314`)
- Bug in :meth:`DataFrame.size` and :meth:`Series.size` returning 64-bit integer instead of int (:issue:`52897`)
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
- 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`)
-


Conversion
^^^^^^^^^^
- 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`)
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,8 @@ def size(self) -> int:
>>> df.size
4
"""
# error: Incompatible return value type (got "signedinteger[_64Bit]",
# expected "int") [return-value]
return np.prod(self.shape) # type: ignore[return-value]

return int(np.prod(self.shape))

def set_axis(
self,
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/frame/methods/test_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np
import pytest

from pandas import DataFrame


@pytest.mark.parametrize(
"data, index, expected",
[
({"col1": [1], "col2": [3]}, None, 2),
({}, None, 0),
({"col1": [1, np.nan], "col2": [3, 4]}, None, 4),
({"col1": [1, 2], "col2": [3, 4]}, [["a", "b"], [1, 2]], 4),
({"col1": [1, 2, 3, 4], "col2": [3, 4, 5, 6]}, ["x", "y", "a", "b"], 8),
],
)
def test_size(data, index, expected):
# GH#52897
df = DataFrame(data, index=index)
assert df.size == expected
assert isinstance(df.size, int)
22 changes: 22 additions & 0 deletions pandas/tests/series/methods/test_size.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from pandas import Series


@pytest.mark.parametrize(
"data, index, expected",
[
([1, 2, 3], None, 3),
({"a": 1, "b": 2, "c": 3}, None, 3),
([1, 2, 3], ["x", "y", "z"], 3),
([1, 2, 3, 4, 5], ["x", "y", "z", "w", "n"], 5),
([1, 2, 3], None, 3),
([1, 2, 3], ["x", "y", "z"], 3),
([1, 2, 3, 4], ["x", "y", "z", "w"], 4),
],
)
def test_series(data, index, expected):
# GH#52897
ser = Series(data, index=index)
assert ser.size == expected
assert isinstance(ser.size, int)