From bc93f039b9bce13f391d7991f8badf7f68ee1e62 Mon Sep 17 00:00:00 2001 From: srkds Date: Sat, 27 May 2023 20:41:01 +0530 Subject: [PATCH 1/5] BUG:series sum changes dtype uint64 to int64 --- pandas/core/nanops.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index b405447239a7a..383077737397c 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -640,6 +640,8 @@ def nansum( dtype_sum = dtype elif dtype.kind == "m": dtype_sum = np.dtype(np.float64) + elif dtype.kind == "u": + dtype_sum = np.dtype(np.uint64) the_sum = values.sum(axis, dtype=dtype_sum) the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count) From 80e78329381892692da452f7d119ca229d547e50 Mon Sep 17 00:00:00 2001 From: srkds Date: Sun, 28 May 2023 20:32:15 +0530 Subject: [PATCH 2/5] condition check should be at _get_dtype_max --- pandas/core/nanops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 383077737397c..1b8a62d1ee0fb 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -324,8 +324,10 @@ def _get_values( def _get_dtype_max(dtype: np.dtype) -> np.dtype: # return a platform independent precision dtype dtype_max = dtype - if dtype.kind in "biu": + if dtype.kind in "bi": dtype_max = np.dtype(np.int64) + elif dtype.kind == "u": + dtype_max = np.dtype(np.uint64) elif dtype.kind == "f": dtype_max = np.dtype(np.float64) return dtype_max @@ -640,8 +642,6 @@ def nansum( dtype_sum = dtype elif dtype.kind == "m": dtype_sum = np.dtype(np.float64) - elif dtype.kind == "u": - dtype_sum = np.dtype(np.uint64) the_sum = values.sum(axis, dtype=dtype_sum) the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count) From fdd8f33954c2e194ee5fa3a2c4ee3ccc59e80674 Mon Sep 17 00:00:00 2001 From: srkds Date: Sun, 28 May 2023 20:33:40 +0530 Subject: [PATCH 3/5] add test for series uint64 sum method --- pandas/tests/series/methods/test_sum.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 pandas/tests/series/methods/test_sum.py diff --git a/pandas/tests/series/methods/test_sum.py b/pandas/tests/series/methods/test_sum.py new file mode 100644 index 0000000000000..4a5f5dd59a4da --- /dev/null +++ b/pandas/tests/series/methods/test_sum.py @@ -0,0 +1,13 @@ +import numpy as np + +from pandas import Series +import pandas._testing as tm + + +class TestSeriesSum: + def test_sum_uint64(self): + # GH 53401 + s = Series([10000000000000000000], dtype="uint64") + result = s.sum() + expected = np.uint64(10000000000000000000) + tm.assert_almost_equal(result, expected) From d59134db0a30e1d73a05f1e1bee5da80f26347ed Mon Sep 17 00:00:00 2001 From: srkds Date: Sun, 28 May 2023 20:34:58 +0530 Subject: [PATCH 4/5] DOC: add entry in whatsnew v2.1.0 --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 2c5263f447951..92387e63ed782 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -351,6 +351,7 @@ Numeric - Bug in :meth:`Series.any`, :meth:`Series.all`, :meth:`DataFrame.any`, and :meth:`DataFrame.all` had the default value of ``bool_only`` set to ``None`` instead of ``False``; this change should have no impact on users (:issue:`53258`) - 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`) +- Bug in :meth:`Series.sum` converting dtype ``uint64`` to ``int64`` (:issue:`53401`) Conversion From d999ee49d4712d7e7910a67e0b1f82bf14214b00 Mon Sep 17 00:00:00 2001 From: srkds Date: Mon, 29 May 2023 14:59:47 +0530 Subject: [PATCH 5/5] series tests should be at test_reductions file --- pandas/tests/reductions/test_reductions.py | 7 +++++++ pandas/tests/series/methods/test_sum.py | 13 ------------- 2 files changed, 7 insertions(+), 13 deletions(-) delete mode 100644 pandas/tests/series/methods/test_sum.py diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 5fa341c9eb74a..b02f8a19c77cb 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1110,6 +1110,13 @@ def test_idxminmax_with_inf(self): assert s.idxmax() == 0 np.isnan(s.idxmax(skipna=False)) + def test_sum_uint64(self): + # GH 53401 + s = Series([10000000000000000000], dtype="uint64") + result = s.sum() + expected = np.uint64(10000000000000000000) + tm.assert_almost_equal(result, expected) + class TestDatetime64SeriesReductions: # Note: the name TestDatetime64SeriesReductions indicates these tests diff --git a/pandas/tests/series/methods/test_sum.py b/pandas/tests/series/methods/test_sum.py deleted file mode 100644 index 4a5f5dd59a4da..0000000000000 --- a/pandas/tests/series/methods/test_sum.py +++ /dev/null @@ -1,13 +0,0 @@ -import numpy as np - -from pandas import Series -import pandas._testing as tm - - -class TestSeriesSum: - def test_sum_uint64(self): - # GH 53401 - s = Series([10000000000000000000], dtype="uint64") - result = s.sum() - expected = np.uint64(10000000000000000000) - tm.assert_almost_equal(result, expected)