-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
API: allow nan-likes in StringArray constructor #41412
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
Changes from 1 commit
3e1784d
96ff1da
418e1d2
25a6c4d
47d68f7
8257dbd
922436a
2f28086
3ee2198
9426a52
3ee55f2
fe4981a
a66948a
e852719
91b73bb
42ec3e4
41f49d2
62cc5be
29909f3
153b6b4
b27a839
ed5b953
caa5705
2d75031
52a00d1
8dc0b66
1bacaed
66be087
7b058cd
1be1bdf
3c57094
03738a9
12351de
889829a
358000f
c649b1d
5e5aa9c
eb7d8f2
2426319
20817a7
33d8f9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,7 @@ class StringArray(PandasArray): | |
|
||
Currently, this expects an object-dtype ndarray | ||
where the elements are Python strings | ||
or nan-likes(``None``, ``nan``, ``NaT``, ``NA``, Decimal("NaN")). | ||
or nan-likes(``None``, ``nan``, ``NA``). | ||
This may change without warning in the future. Use | ||
:meth:`pandas.array` with ``dtype="string"`` for a stable way of | ||
creating a `StringArray` from any sequence. | ||
|
@@ -239,23 +239,33 @@ def _validate(self): | |
raise ValueError("StringArray requires a sequence of strings or pandas.NA") | ||
|
||
@classmethod | ||
def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy=False): | ||
def _from_sequence( | ||
cls, scalars, *, dtype: Dtype | None = None, copy=False, coerce=True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this is like but I still think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will take a look soon-ish. I'm wary of adding keywords here |
||
): | ||
if dtype: | ||
assert dtype == "string" | ||
|
||
from pandas.core.arrays.masked import BaseMaskedArray | ||
|
||
if isinstance(scalars, BaseMaskedArray): | ||
# avoid costly conversion to object dtype | ||
if coerce: | ||
coerce = "non-null" | ||
else: | ||
coerce = None | ||
na_values = scalars._mask | ||
result = scalars._data | ||
result = lib.ensure_string_array(result, copy=copy, coerce="non-null") | ||
result = lib.ensure_string_array(result, copy=copy, coerce=coerce) | ||
result[na_values] = StringDtype.na_value | ||
|
||
else: | ||
# convert non-na-likes to str, and nan-likes to StringDtype.na_value | ||
if coerce: | ||
coerce = "all" | ||
else: | ||
coerce = "strict-null" | ||
result = lib.ensure_string_array( | ||
scalars, na_value=StringDtype.na_value, copy=copy | ||
scalars, na_value=StringDtype.na_value, copy=copy, coerce=coerce | ||
) | ||
|
||
# Manually creating new array avoids the validation step in the __init__, so is | ||
|
Uh oh!
There was an error while loading. Please reload this page.