Skip to content

BUG: Float64Index.astype('u8') returns Int64Index #45309

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 4 commits into from
Jan 16, 2022
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/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ Numeric
Conversion
^^^^^^^^^^
- Bug in constructing a :class:`Series` from a float-containing list or a floating-dtype ndarray-like (e.g. ``dask.Array``) and an integer dtype raising instead of casting like we would with an ``np.ndarray`` (:issue:`40110`)
- Bug in :meth:`Float64Index.astype` to unsigned integer dtype incorrectly casting to ``np.int64`` dtype (:issue:`45309`)
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` from floating dtype to unsigned integer dtype failing to raise in the presence of negative values (:issue:`45151`)
-

Strings
Expand Down
13 changes: 9 additions & 4 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,19 @@ def all_timeseries_index_generator(k: int = 10) -> Iterable[Index]:


# make series
def makeFloatSeries(name=None):
def make_rand_series(name=None, dtype=np.float64):
index = makeStringIndex(_N)
return Series(np.random.randn(_N), index=index, name=name)
data = np.random.randn(_N)
data = data.astype(dtype, copy=False)
return Series(data, index=index, name=name)


def makeFloatSeries(name=None):
return make_rand_series(name=name)


def makeStringSeries(name=None):
index = makeStringIndex(_N)
return Series(np.random.randn(_N), index=index, name=name)
return make_rand_series(name=name)


def makeObjectSeries(name=None):
Expand Down
2 changes: 1 addition & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def series_with_multilevel_index():


_narrow_series = {
f"{dtype.__name__}-series": tm.makeFloatSeries(name="a").astype(dtype)
f"{dtype.__name__}-series": tm.make_rand_series(name="a", dtype=dtype)
for dtype in tm.NARROW_NP_DTYPES
}

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
npt,
)
from pandas.compat.numpy import function as nv
from pandas.errors import IntCastingNaNError
from pandas.util._decorators import Appender

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -906,7 +907,12 @@ def astype(self, dtype, copy: bool = True):
# np.nan entries to int subtypes
new_left = Index(self._left, copy=False).astype(dtype.subtype)
new_right = Index(self._right, copy=False).astype(dtype.subtype)
except TypeError as err:
except IntCastingNaNError:
# e.g test_subtype_integer
raise
except (TypeError, ValueError) as err:
# e.g. test_subtype_integer_errors f8->u8 can be lossy
# and raises ValueError
msg = (
f"Cannot convert {self.dtype} to {dtype}; subtypes are incompatible"
)
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def _astype_float_to_int_nansafe(
raise IntCastingNaNError(
"Cannot convert non-finite values (NA or inf) to integer"
)
if dtype.kind == "u":
# GH#45151
if not (values >= 0).all():
raise ValueError(f"Cannot losslessly cast from {values.dtype} to {dtype}")
return values.astype(dtype, copy=copy)


Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ def astype(self, dtype, copy: bool = True):
# GH 13149
arr = astype_nansafe(self._values, dtype=dtype)
if isinstance(self, Float64Index):
return Int64Index(arr, name=self.name)
if dtype.kind == "i":
return Int64Index(arr, name=self.name)
else:
return UInt64Index(arr, name=self.name)
else:
return NumericIndex(arr, name=self.name, dtype=dtype)
elif self._is_backward_compat_public_numeric_index:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_memory_usage_components_series(series_with_simple_index):

@pytest.mark.parametrize("dtype", tm.NARROW_NP_DTYPES)
def test_memory_usage_components_narrow_series(dtype):
series = tm.makeFloatSeries(name="a").astype(dtype)
series = tm.make_rand_series(name="a", dtype=dtype)
total_usage = series.memory_usage(index=True)
non_index_usage = series.memory_usage(index=False)
index_usage = series.index.memory_usage()
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/indexes/interval/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import numpy as np
import pytest

from pandas.compat import is_platform_arm

from pandas.core.dtypes.dtypes import (
CategoricalDtype,
IntervalDtype,
Expand Down Expand Up @@ -170,7 +168,6 @@ def test_subtype_integer_with_non_integer_borders(self, subtype):
)
tm.assert_index_equal(result, expected)

@pytest.mark.xfail(is_platform_arm(), reason="GH 41740")
def test_subtype_integer_errors(self):
# float64 -> uint64 fails with negative values
index = interval_range(-10.0, 10.0)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/numeric/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@
from pandas.core.indexes.api import (
Float64Index,
Int64Index,
UInt64Index,
)


class TestAstype:
def test_astype_float64_to_uint64(self):
# GH#45309 used to incorrectly return Int64Index
idx = Float64Index([0.0, 5.0, 10.0, 15.0, 20.0])
result = idx.astype("u8")
expected = UInt64Index([0, 5, 10, 15, 20])
tm.assert_index_equal(result, expected)

idx_with_negatives = idx - 10
with pytest.raises(ValueError, match="losslessly"):
idx_with_negatives.astype(np.uint64)

def test_astype_float64_to_object(self):
float_index = Float64Index([0.0, 2.5, 5.0, 7.5, 10.0])
result = float_index.astype(object)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ def test_astype_cast_object_int_fail(self, dtype):
with pytest.raises(ValueError, match=msg):
arr.astype(dtype)

def test_astype_float_to_uint_negatives_raise(
self, float_numpy_dtype, any_unsigned_int_numpy_dtype
):
# GH#45151
# TODO: same for EA float/uint dtypes
arr = np.arange(5).astype(float_numpy_dtype) - 3 # includes negatives
ser = Series(arr)

with pytest.raises(ValueError, match="losslessly"):
ser.astype(any_unsigned_int_numpy_dtype)

def test_astype_cast_object_int(self):
arr = Series(["1", "2", "3", "4"], dtype=object)
result = arr.astype(int)
Expand Down