We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0db3112 commit 494c108Copy full SHA for 494c108
doc/source/whatsnew/v1.0.2.rst
@@ -34,8 +34,10 @@ Bug fixes
34
35
- Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`)
36
37
+
38
**Experimental dtypes**
39
40
+- Fix bug in :meth:`DataFrame.convert_dtypes` for columns that were already using the ``"string"`` dtype (:issue:`31731`).
41
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
42
43
.. ---------------------------------------------------------------------------
pandas/_libs/lib.pyx
@@ -961,7 +961,7 @@ _TYPE_MAP = {
961
'complex64': 'complex',
962
'complex128': 'complex',
963
'c': 'complex',
964
- 'string': 'bytes',
+ 'string': 'string',
965
'S': 'bytes',
966
'U': 'string',
967
'bool': 'boolean',
pandas/conftest.py
@@ -731,6 +731,7 @@ def any_numpy_dtype(request):
731
# categoricals are handled separately
732
_any_skipna_inferred_dtype = [
733
("string", ["a", np.nan, "c"]),
734
+ ("string", ["a", pd.NA, "c"]),
735
("bytes", [b"a", np.nan, b"c"]),
736
("empty", [np.nan, np.nan, np.nan]),
737
("empty", []),
@@ -741,6 +742,7 @@ def any_numpy_dtype(request):
741
742
("mixed-integer-float", [1, np.nan, 2.0]),
743
("decimal", [Decimal(1), np.nan, Decimal(2)]),
744
("boolean", [True, np.nan, False]),
745
+ ("boolean", [True, pd.NA, False]),
746
("datetime64", [np.datetime64("2013-01-01"), np.nan, np.datetime64("2018-01-01")]),
747
("datetime", [pd.Timestamp("20130101"), np.nan, pd.Timestamp("20180101")]),
748
("date", [date(2013, 1, 1), np.nan, date(2018, 1, 1)]),
pandas/tests/dtypes/test_inference.py
@@ -1200,6 +1200,24 @@ def test_interval(self):
1200
inferred = lib.infer_dtype(pd.Series(idx), skipna=False)
1201
assert inferred == "interval"
1202
1203
+ @pytest.mark.parametrize("klass", [pd.array, pd.Series])
1204
+ @pytest.mark.parametrize("skipna", [True, False])
1205
+ @pytest.mark.parametrize("data", [["a", "b", "c"], ["a", "b", pd.NA]])
1206
+ def test_string_dtype(self, data, skipna, klass):
1207
+ # StringArray
1208
+ val = klass(data, dtype="string")
1209
+ inferred = lib.infer_dtype(val, skipna=skipna)
1210
+ assert inferred == "string"
1211
1212
1213
1214
+ @pytest.mark.parametrize("data", [[True, False, True], [True, False, pd.NA]])
1215
+ def test_boolean_dtype(self, data, skipna, klass):
1216
+ # BooleanArray
1217
+ val = klass(data, dtype="boolean")
1218
1219
+ assert inferred == "boolean"
1220
1221
1222
class TestNumberScalar:
1223
def test_is_number(self):
pandas/tests/series/test_convert_dtypes.py
@@ -246,3 +246,12 @@ def test_convert_dtypes(self, data, maindtype, params, answerdict):
246
247
# Make sure original not changed
248
tm.assert_series_equal(series, copy)
249
250
+ def test_convert_string_dtype(self):
251
+ # https://github.com/pandas-dev/pandas/issues/31731 -> converting columns
252
+ # that are already string dtype
253
+ df = pd.DataFrame(
254
+ {"A": ["a", "b", pd.NA], "B": ["ä", "ö", "ü"]}, dtype="string"
255
+ )
256
+ result = df.convert_dtypes()
257
+ tm.assert_frame_equal(df, result)
pandas/tests/test_strings.py
@@ -7,6 +7,7 @@
7
8
from pandas._libs import lib
9
10
+import pandas as pd
11
from pandas import DataFrame, Index, MultiIndex, Series, concat, isna, notna
12
import pandas._testing as tm
13
import pandas.core.strings as strings
@@ -207,6 +208,9 @@ def test_api_per_dtype(self, index_or_series, dtype, any_skipna_inferred_dtype):
207
208
box = index_or_series
209
inferred_dtype, values = any_skipna_inferred_dtype
210
211
+ if dtype == "category" and len(values) and values[1] is pd.NA:
212
+ pytest.xfail(reason="Categorical does not yet support pd.NA")
213
214
t = box(values, dtype=dtype) # explicit dtype to avoid casting
215
216
# TODO: get rid of these xfails
0 commit comments