Skip to content

Commit 118a610

Browse files
committed
add default for dtype in init
1 parent 0f37213 commit 118a610

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

pandas/core/arrays/string_.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,11 @@ class StringArray(BaseStringArray, NumpyExtensionArray): # type: ignore[misc]
646646
# undo the NumpyExtensionArray hack
647647
_typ = "extension"
648648

649-
def __init__(self, values, *, dtype: StringDtype, copy: bool = False) -> None:
649+
def __init__(
650+
self, values, *, dtype: StringDtype = None, copy: bool = False
651+
) -> None:
652+
if dtype is None:
653+
dtype = StringDtype()
650654
values = extract_array(values)
651655

652656
super().__init__(values, copy=copy)
@@ -758,7 +762,7 @@ def _cast_pointwise_result(self, values) -> ArrayLike:
758762
@classmethod
759763
def _empty(cls, shape, dtype) -> StringArray:
760764
values = np.empty(shape, dtype=object)
761-
values[:] = libmissing.NA
765+
values[:] = dtype.na_value
762766
return cls(values, dtype=dtype).astype(dtype, copy=False)
763767

764768
def __arrow_array__(self, type=None):

pandas/tests/io/parser/test_upcast.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
BooleanArray,
1515
FloatingArray,
1616
IntegerArray,
17-
StringArray,
1817
)
1918

2019

@@ -95,10 +94,7 @@ def test_maybe_upcast_object(val, string_storage):
9594

9695
if string_storage == "python":
9796
exp_val = "c" if val == "c" else NA
98-
dtype = pd.StringDtype()
99-
expected = StringArray(
100-
np.array(["a", "b", exp_val], dtype=np.object_), dtype=dtype
101-
)
97+
expected = pd.array(["a", "b", exp_val], dtype=pd.StringDtype())
10298
else:
10399
exp_val = "c" if val == "c" else None
104100
expected = ArrowStringArray(pa.array(["a", "b", exp_val]))

pandas/tests/io/test_orc.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pandas as pd
1313
from pandas import read_orc
1414
import pandas._testing as tm
15-
from pandas.core.arrays import StringArray
1615

1716
pytest.importorskip("pyarrow.orc")
1817

@@ -368,15 +367,9 @@ def test_orc_dtype_backend_numpy_nullable():
368367

369368
expected = pd.DataFrame(
370369
{
371-
"string": StringArray(
372-
np.array(["a", "b", "c"], dtype=np.object_), dtype=pd.StringDtype()
373-
),
374-
"string_with_nan": StringArray(
375-
np.array(["a", pd.NA, "c"], dtype=np.object_), dtype=pd.StringDtype()
376-
),
377-
"string_with_none": StringArray(
378-
np.array(["a", pd.NA, "c"], dtype=np.object_), dtype=pd.StringDtype()
379-
),
370+
"string": pd.array(["a", "b", "c"], dtype=pd.StringDtype()),
371+
"string_with_nan": pd.array(["a", pd.NA, "c"], dtype=pd.StringDtype()),
372+
"string_with_none": pd.array(["a", pd.NA, "c"], dtype=pd.StringDtype()),
380373
"int": pd.Series([1, 2, 3], dtype="Int64"),
381374
"int_with_nan": pd.Series([1, pd.NA, 3], dtype="Int64"),
382375
"na_only": pd.Series([pd.NA, pd.NA, pd.NA], dtype="Int64"),

0 commit comments

Comments
 (0)