Skip to content

Commit

Permalink
fail on float16, but allow np.exp(int8_arrays) II
Browse files Browse the repository at this point in the history
  • Loading branch information
Terji Petersen authored and Terji Petersen committed Dec 10, 2022
1 parent a440da5 commit aa8b577
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@ def value_counts(
else:
values = _ensure_arraylike(values)
keys, counts = value_counts_arraylike(values, dropna)
if keys.dtype == np.float16:
keys = keys.astype(np.float32)

# For backwards compatibility, we let Index do its normal type
# inference, _except_ for if if infers from object to bool.
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,8 @@ def factorize(
codes, uniques = algorithms.factorize(
self._values, sort=sort, use_na_sentinel=use_na_sentinel
)
if uniques.dtype == np.float16:
uniques = uniques.astype(np.float32)

if isinstance(self, ABCIndex):
# preserve e.g. NumericIndex, preserve MultiIndex
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def test_factorize(self, index_or_series_obj, sort):
constructor = Index
if isinstance(obj, MultiIndex):
constructor = MultiIndex.from_tuples
expected_uniques = constructor(obj.unique())
expected_arr = obj.unique()
if expected_arr.dtype == np.float16:
expected_arr = expected_arr.astype(np.float32)
expected_uniques = constructor(expected_arr)
if (
isinstance(obj, Index)
and expected_uniques.dtype == bool
Expand Down

0 comments on commit aa8b577

Please sign in to comment.