Skip to content

Commit f316e42

Browse files
committed
changes
1 parent 681a211 commit f316e42

File tree

6 files changed

+14
-5
lines changed

6 files changed

+14
-5
lines changed

pandas/core/arrays/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ def astype(self, dtype, copy=True):
456456
dtype = pandas_dtype(dtype)
457457
if isinstance(dtype, StringDtype):
458458
return dtype.construct_array_type()._from_sequence(self, copy=False)
459+
459460
return np.array(self, dtype=dtype, copy=copy)
460461

461462
def isna(self) -> ArrayLike:

pandas/core/arrays/period.py

+1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ def _format_native_types(self, na_rep="NaT", date_format=None, **kwargs):
564564
actually format my specific types
565565
"""
566566
values = self.astype(object)
567+
567568
if date_format:
568569
formatter = lambda dt: dt.strftime(date_format)
569570
else:

pandas/core/arrays/sparse/dtype.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas.core.dtypes.cast import astype_nansafe
1414
from pandas.core.dtypes.common import (
1515
is_bool_dtype,
16+
is_extension_array_dtype,
1617
is_object_dtype,
1718
is_scalar,
1819
is_string_dtype,
@@ -322,7 +323,10 @@ def update_dtype(self, dtype):
322323
dtype = pandas_dtype(dtype)
323324

324325
if not isinstance(dtype, cls):
325-
fill_value = astype_nansafe(np.array([self.fill_value]), dtype)[0]
326+
if is_extension_array_dtype(dtype):
327+
raise TypeError("sparse arrays of extension dtypes not supported")
328+
329+
fill_value = astype_nansafe(np.array(self.fill_value), dtype).item()
326330
dtype = cls(dtype, fill_value=fill_value)
327331

328332
return dtype

pandas/tests/arrays/sparse/test_array.py

+1
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ def test_astype_all(self, any_real_dtype):
542542
np.array([0, 1], dtype="datetime64[ns]"),
543543
dtype=SparseDtype("datetime64[ns]", pd.Timestamp("1970")),
544544
),
545+
marks=[pytest.mark.xfail(reason="NumPy-7619")],
545546
),
546547
(
547548
SparseArray([0, 1, 10]),

pandas/tests/extension/test_numpy.py

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def test_astype_str(self, data):
141141

142142
@skip_nested
143143
def test_astype_string(self, data):
144+
# GH-33465
144145
# ValueError: setting an array element with a sequence
145146
super().test_astype_string(data)
146147

pandas/tests/extension/test_sparse.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,13 @@ def test_astype_object_frame(self, all_data):
343343
# comp = result.dtypes.equals(df.dtypes)
344344
# assert not comp.any()
345345

346-
@pytest.mark.xfail(raises=AssertionError, reason="no sparse str dtype")
347346
def test_astype_str(self, data):
348-
# Sparse arrays don't support str dtype
349-
super().test_astype_str(data)
347+
result = pd.Series(data[:5]).astype(str)
348+
expected_dtype = pd.SparseDtype(str, str(data.fill_value))
349+
expected = pd.Series([str(x) for x in data[:5]], dtype=expected_dtype)
350+
self.assert_series_equal(result, expected)
350351

351-
@pytest.mark.xfail(raises=AssertionError, reason="no sparse StringDtype")
352+
@pytest.mark.xfail(raises=TypeError, reason="no sparse StringDtype")
352353
def test_astype_string(self, data):
353354
super().test_astype_string(data)
354355

0 commit comments

Comments
 (0)