Skip to content

[ArrowStringArray] BUG: fix test_astype_string for Float32Dtype #40998

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 17 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ def astype(self, dtype, copy: bool = True):
"""
from pandas import Index
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import ArrowStringDtype

if dtype is not None:
dtype = pandas_dtype(dtype)
Expand All @@ -851,7 +852,7 @@ def astype(self, dtype, copy: bool = True):
return self._shallow_copy(new_left, new_right)
elif is_categorical_dtype(dtype):
return Categorical(np.asarray(self), dtype=dtype)
elif isinstance(dtype, StringDtype):
elif isinstance(dtype, (StringDtype, ArrowStringDtype)):
return dtype.construct_array_type()._from_sequence(self, copy=False)

# TODO: This try/except will be repeated.
Expand Down
18 changes: 15 additions & 3 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,22 @@ def _chk_pyarrow_available(cls) -> None:

@classmethod
def _from_sequence(cls, scalars, dtype: Dtype | None = None, copy: bool = False):
from pandas.core.arrays.masked import BaseMaskedArray

cls._chk_pyarrow_available()
# convert non-na-likes to str, and nan-likes to ArrowStringDtype.na_value
scalars = lib.ensure_string_array(scalars, copy=False)
return cls(pa.array(scalars, type=pa.string(), from_pandas=True))

if isinstance(scalars, BaseMaskedArray):
# avoid costly conversion to object dtype in ensure_string_array and
# numerical issues with Float32Dtype
na_values = scalars._mask
result = scalars._data
result = lib.ensure_string_array(result, copy=False, convert_na_value=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

umm this doesn't copy? (its fine but then you are mutating)

Copy link
Member Author

Choose a reason for hiding this comment

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

that's not a change in this PR. will need to look back why we do this.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated to be consistent with StringArray. I guess that the assumption was that a copy would probably have been created in most cases (unless an object array was passed) and explicitly avoid the additional copy made in ensure_string_array since that does not track if a copy/new array has been already been created before making a copy. The default for copy is False.

result[na_values] = ArrowStringDtype.na_value
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, might also be able to do pa.array(result, mask=mask, type=pa.string()), then pyarrow will use the mask for missing values, and we don't need the setitem operation.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

else:
# convert non-na-likes to str, and nan-likes to StringDtype.na_value
result = lib.ensure_string_array(scalars, copy=False)

return cls(pa.array(result, type=pa.string(), from_pandas=True))

@classmethod
def _from_sequence_of_strings(
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/extension/base/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def test_astype_str(self, data):
expected = pd.Series([str(x) for x in data[:5]], dtype=str)
self.assert_series_equal(result, expected)

def test_astype_string(self, data):
def test_astype_string(self, data, nullable_string_dtype):
# GH-33465
result = pd.Series(data[:5]).astype("string")
expected = pd.Series([str(x) for x in data[:5]], dtype="string")
result = pd.Series(data[:5]).astype(nullable_string_dtype)
expected = pd.Series([str(x) for x in data[:5]], dtype=nullable_string_dtype)
self.assert_series_equal(result, expected)

def test_to_numpy(self, data):
Expand Down