Skip to content

Commit b34b600

Browse files
committed
fixup
1 parent 8ba72d1 commit b34b600

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

pandas/core/arrays/numpy_.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,32 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
162162
result = np.asarray(scalars, dtype=dtype)
163163
if copy and result is scalars:
164164
result = result.copy()
165-
# Primarily for StringArray
166-
result = cls._from_sequence_finalize(result, copy=copy)
165+
# _coerce_from_sequence_values is useful for StringArray
166+
result = cls._coerce_from_sequence_values(result, copy=copy)
167167
return cls(result)
168168

169169
@staticmethod
170-
def _from_sequence_finalize(values, copy):
170+
def _coerce_from_sequence_values(values: np.ndarray, copy: bool):
171+
"""
172+
Coerce the values used in _from_sequence before constructing
173+
174+
This may mutate `values` inplace.
175+
176+
Parameters
177+
----------
178+
values : ndarray
179+
The values to coerce, probably from asarray on the sequence
180+
passed to _from_sequence.
181+
copy : bool
182+
This should be the value of `copy` passed to _from_sequence.
183+
It's used to determine if we potentially haven't done a
184+
copy, and so a copy will be needed to not mutate the user input.
185+
186+
Returns
187+
-------
188+
numpy.ndarray
189+
The coerced values.
190+
"""
171191
return values
172192

173193
@classmethod

pandas/core/arrays/string_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
182182
return result
183183

184184
@staticmethod
185-
def _from_sequence_finalize(values, copy):
185+
def _coerce_from_sequence_values(values: np.ndarray, copy: bool):
186186
# Standardize all missing-like values to NA
187187
# TODO: it would be nice to do this in _validate / lib.is_string_array
188188
# We are already doing a scan over the values there.

pandas/tests/arrays/string_/test_string.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,11 @@ def test_constructor_raises():
204204
pd.arrays.StringArray(np.array(["a", pd.NaT], dtype=object))
205205

206206

207-
def test_from_sequnce_no_mutate():
207+
@pytest.mark.parametrize("copy", [True, False])
208+
def test_from_sequnce_no_mutate(copy):
208209
a = np.array(["a", np.nan], dtype=object)
209210
original = a.copy()
210-
result = pd.arrays.StringArray._from_sequence(a)
211+
result = pd.arrays.StringArray._from_sequence(a, copy=copy)
211212
expected = pd.arrays.StringArray(np.array(["a", pd.NA], dtype=object))
212213
tm.assert_extension_array_equal(result, expected)
213214
tm.assert_numpy_array_equal(a, original)

0 commit comments

Comments
 (0)