Skip to content

Commit 5466f15

Browse files
authored
Backport PR #58202: DOC/TST: Document numpy 2.0 support and add tests… (#58208)
Backport PR #58202: DOC/TST: Document numpy 2.0 support and add tests for string array
1 parent 45b0b32 commit 5466f15

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

doc/source/whatsnew/v2.2.2.rst

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ including other versions of pandas.
99
{{ header }}
1010

1111
.. ---------------------------------------------------------------------------
12+
13+
.. _whatsnew_220.np2_compat:
14+
15+
Pandas 2.2.2 is now compatible with numpy 2.0
16+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
18+
Pandas 2.2.2 is the first version of pandas that is generally compatible with the upcoming
19+
numpy 2.0 release, and wheels for pandas 2.2.2 will work with both numpy 1.x and 2.x.
20+
21+
One major caveat is that arrays created with numpy 2.0's new ``StringDtype`` will convert
22+
to ``object`` dtyped arrays upon :class:`Series`/:class:`DataFrame` creation.
23+
Full support for numpy 2.0's StringDtype is expected to land in pandas 3.0.
24+
25+
As usual please report any bugs discovered to our `issue tracker <https://github.com/pandas-dev/pandas/issues/new/choose>`_
26+
1227
.. _whatsnew_222.regressions:
1328

1429
Fixed regressions

pandas/tests/frame/test_constructors.py

+19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pandas._config import using_pyarrow_string_dtype
2525

2626
from pandas._libs import lib
27+
from pandas.compat.numpy import np_version_gt2
2728
from pandas.errors import IntCastingNaNError
2829
import pandas.util._test_decorators as td
2930

@@ -3118,6 +3119,24 @@ def test_columns_indexes_raise_on_sets(self):
31183119
with pytest.raises(ValueError, match="columns cannot be a set"):
31193120
DataFrame(data, columns={"a", "b", "c"})
31203121

3122+
# TODO: make this not cast to object in pandas 3.0
3123+
@pytest.mark.skipif(
3124+
not np_version_gt2, reason="StringDType only available in numpy 2 and above"
3125+
)
3126+
@pytest.mark.parametrize(
3127+
"data",
3128+
[
3129+
{"a": ["a", "b", "c"], "b": [1.0, 2.0, 3.0], "c": ["d", "e", "f"]},
3130+
],
3131+
)
3132+
def test_np_string_array_object_cast(self, data):
3133+
from numpy.dtypes import StringDType
3134+
3135+
data["a"] = np.array(data["a"], dtype=StringDType())
3136+
res = DataFrame(data)
3137+
assert res["a"].dtype == np.object_
3138+
assert (res["a"] == data["a"]).all()
3139+
31213140

31223141
def get1(obj): # TODO: make a helper in tm?
31233142
if isinstance(obj, Series):

pandas/tests/series/test_constructors.py

+19
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,25 @@ def test_series_constructor_infer_multiindex(self, container, data):
21912191
multi = Series(data, index=indexes)
21922192
assert isinstance(multi.index, MultiIndex)
21932193

2194+
# TODO: make this not cast to object in pandas 3.0
2195+
@pytest.mark.skipif(
2196+
not np_version_gt2, reason="StringDType only available in numpy 2 and above"
2197+
)
2198+
@pytest.mark.parametrize(
2199+
"data",
2200+
[
2201+
["a", "b", "c"],
2202+
["a", "b", np.nan],
2203+
],
2204+
)
2205+
def test_np_string_array_object_cast(self, data):
2206+
from numpy.dtypes import StringDType
2207+
2208+
arr = np.array(data, dtype=StringDType())
2209+
res = Series(arr)
2210+
assert res.dtype == np.object_
2211+
assert (res == data).all()
2212+
21942213

21952214
class TestSeriesConstructorInternals:
21962215
def test_constructor_no_pandas_array(self, using_array_manager):

0 commit comments

Comments
 (0)