Skip to content

BUG fix IntegerArray.astype int -> uint #22441

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 2 commits into from
Aug 22, 2018
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: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Pandas has gained the ability to hold integer dtypes with missing values. This l
Here is an example of the usage.

We can construct a ``Series`` with the specified dtype. The dtype string ``Int64`` is a pandas ``ExtensionDtype``. Specifying a list or array using the traditional missing value
marker of ``np.nan`` will infer to integer dtype. The display of the ``Series`` will also use the ``NaN`` to indicate missing values in string outputs. (:issue:`20700`, :issue:`20747`)
marker of ``np.nan`` will infer to integer dtype. The display of the ``Series`` will also use the ``NaN`` to indicate missing values in string outputs. (:issue:`20700`, :issue:`20747`, :issue:`22441`)

.. ipython:: python

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ def astype(self, dtype, copy=True):

# if we are astyping to an existing IntegerDtype we can fastpath
if isinstance(dtype, _IntegerDtype):
result = self._data.astype(dtype.numpy_dtype,
casting='same_kind', copy=False)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback do you remember what the reason was to explicitly put casting='same_kind' ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc was to make sure we were
only coercing actual ints (and would raise on floats)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But since we have the if isinstance(dtype, _IntegerDtype):, we are sure about that? (so it can never be a float dtype)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might have been an artificat.

result = self._data.astype(dtype.numpy_dtype, copy=False)
return type(self)(result, mask=self._mask, copy=False)

# coerce
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/extension/integer/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,16 +566,17 @@ def test_astype(self, all_data):
expected = pd.Series(np.asarray(mixed))
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize('dtype', [Int8Dtype(), 'Int8'])
@pytest.mark.parametrize('dtype', [Int8Dtype(), 'Int8',
UInt32Dtype(), 'UInt32'])
def test_astype_specific_casting(self, dtype):
s = pd.Series([1, 2, 3], dtype='Int64')
result = s.astype(dtype)
expected = pd.Series([1, 2, 3], dtype='Int8')
expected = pd.Series([1, 2, 3], dtype=dtype)
self.assert_series_equal(result, expected)

s = pd.Series([1, 2, 3, None], dtype='Int64')
result = s.astype(dtype)
expected = pd.Series([1, 2, 3, None], dtype='Int8')
expected = pd.Series([1, 2, 3, None], dtype=dtype)
self.assert_series_equal(result, expected)

def test_construct_cast_invalid(self, dtype):
Expand Down