You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import numpy as np
|
|
import pytest
|
|
|
|
import pandas as pd
|
|
import pandas._testing as tm
|
|
|
|
|
|
def test_astype(using_infer_string):
|
|
# with missing values
|
|
arr = pd.array([True, False, None], dtype="boolean")
|
|
|
|
with pytest.raises(ValueError, match="cannot convert NA to integer"):
|
|
arr.astype("int64")
|
|
|
|
with pytest.raises(ValueError, match="cannot convert float NaN to"):
|
|
arr.astype("bool")
|
|
|
|
result = arr.astype("float64")
|
|
expected = np.array([1, 0, np.nan], dtype="float64")
|
|
tm.assert_numpy_array_equal(result, expected)
|
|
|
|
result = arr.astype("str")
|
|
if using_infer_string:
|
|
expected = pd.array(
|
|
["True", "False", None], dtype=pd.StringDtype(na_value=np.nan)
|
|
)
|
|
tm.assert_extension_array_equal(result, expected)
|
|
else:
|
|
expected = np.array(["True", "False", "<NA>"], dtype=f"{tm.ENDIAN}U5")
|
|
tm.assert_numpy_array_equal(result, expected)
|
|
|
|
# no missing values
|
|
arr = pd.array([True, False, True], dtype="boolean")
|
|
result = arr.astype("int64")
|
|
expected = np.array([1, 0, 1], dtype="int64")
|
|
tm.assert_numpy_array_equal(result, expected)
|
|
|
|
result = arr.astype("bool")
|
|
expected = np.array([True, False, True], dtype="bool")
|
|
tm.assert_numpy_array_equal(result, expected)
|
|
|
|
|
|
def test_astype_to_boolean_array():
|
|
# astype to BooleanArray
|
|
arr = pd.array([True, False, None], dtype="boolean")
|
|
|
|
result = arr.astype("boolean")
|
|
tm.assert_extension_array_equal(result, arr)
|
|
result = arr.astype(pd.BooleanDtype())
|
|
tm.assert_extension_array_equal(result, arr)
|
|
|
|
|
|
def test_astype_to_integer_array():
|
|
# astype to IntegerArray
|
|
arr = pd.array([True, False, None], dtype="boolean")
|
|
|
|
result = arr.astype("Int64")
|
|
expected = pd.array([1, 0, None], dtype="Int64")
|
|
tm.assert_extension_array_equal(result, expected)
|