Skip to content

Commit d8ae9a7

Browse files
authored
BUG: series sum changes dtype uint64 to int64 (#53418)
* BUG:series sum changes dtype uint64 to int64 * condition check should be at _get_dtype_max * add test for series uint64 sum method * DOC: add entry in whatsnew v2.1.0 * series tests should be at test_reductions file
1 parent 59845b7 commit d8ae9a7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ Numeric
351351
- 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`)
352352
- Bug in :meth:`Series.corr` and :meth:`Series.cov` raising ``AttributeError`` for masked dtypes (:issue:`51422`)
353353
- 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`)
354+
- Bug in :meth:`Series.sum` converting dtype ``uint64`` to ``int64`` (:issue:`53401`)
354355

355356

356357
Conversion

pandas/core/nanops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ def _get_values(
324324
def _get_dtype_max(dtype: np.dtype) -> np.dtype:
325325
# return a platform independent precision dtype
326326
dtype_max = dtype
327-
if dtype.kind in "biu":
327+
if dtype.kind in "bi":
328328
dtype_max = np.dtype(np.int64)
329+
elif dtype.kind == "u":
330+
dtype_max = np.dtype(np.uint64)
329331
elif dtype.kind == "f":
330332
dtype_max = np.dtype(np.float64)
331333
return dtype_max

pandas/tests/reductions/test_reductions.py

+7
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,13 @@ def test_idxminmax_with_inf(self):
11101110
assert s.idxmax() == 0
11111111
np.isnan(s.idxmax(skipna=False))
11121112

1113+
def test_sum_uint64(self):
1114+
# GH 53401
1115+
s = Series([10000000000000000000], dtype="uint64")
1116+
result = s.sum()
1117+
expected = np.uint64(10000000000000000000)
1118+
tm.assert_almost_equal(result, expected)
1119+
11131120

11141121
class TestDatetime64SeriesReductions:
11151122
# Note: the name TestDatetime64SeriesReductions indicates these tests

0 commit comments

Comments
 (0)