Skip to content

Commit 0ac13e6

Browse files
mikekutzmaSeeminSyed
authored andcommitted
[BUG] add consistency to_numeric on empty list (pandas-dev#32512)
1 parent b0dac5b commit 0ac13e6

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ Numeric
268268
^^^^^^^
269269
- Bug in :meth:`DataFrame.floordiv` with ``axis=0`` not treating division-by-zero like :meth:`Series.floordiv` (:issue:`31271`)
270270
- Bug in :meth:`to_numeric` with string argument ``"uint64"`` and ``errors="coerce"`` silently fails (:issue:`32394`)
271+
- Bug in :meth:`to_numeric` with ``downcast="unsigned"`` fails for empty data (:issue:`32493`)
271272
-
272273

273274
Conversion

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def to_numeric(arg, errors="raise", downcast=None):
162162

163163
if downcast in ("integer", "signed"):
164164
typecodes = np.typecodes["Integer"]
165-
elif downcast == "unsigned" and np.min(values) >= 0:
165+
elif downcast == "unsigned" and (not len(values) or np.min(values) >= 0):
166166
typecodes = np.typecodes["UnsignedInteger"]
167167
elif downcast == "float":
168168
typecodes = np.typecodes["Float"]

pandas/tests/tools/test_to_numeric.py

+12
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,18 @@ def test_non_coerce_uint64_conflict(errors, exp):
629629
tm.assert_series_equal(result, ser)
630630

631631

632+
@pytest.mark.parametrize("dc1", ["integer", "float", "unsigned"])
633+
@pytest.mark.parametrize("dc2", ["integer", "float", "unsigned"])
634+
def test_downcast_empty(dc1, dc2):
635+
# GH32493
636+
637+
tm.assert_numpy_array_equal(
638+
pd.to_numeric([], downcast=dc1),
639+
pd.to_numeric([], downcast=dc2),
640+
check_dtype=False,
641+
)
642+
643+
632644
def test_failure_to_convert_uint64_string_to_NaN():
633645
# GH 32394
634646
result = to_numeric("uint64", errors="coerce")

0 commit comments

Comments
 (0)