Skip to content

Commit 84bf1ef

Browse files
BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly (pandas-dev#60436)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent f3045db commit 84bf1ef

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

pandas/core/construction.py

+2
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ def sanitize_array(
596596
# create an extension array from its dtype
597597
_sanitize_non_ordered(data)
598598
cls = dtype.construct_array_type()
599+
if not hasattr(data, "__array__"):
600+
data = list(data)
599601
subarr = cls._from_sequence(data, dtype=dtype, copy=copy)
600602

601603
# GH#846

pandas/tests/base/test_constructors.py

+11
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,14 @@ def test_constructor_datetime_nonns(self, constructor):
179179
arr.flags.writeable = False
180180
result = constructor(arr)
181181
tm.assert_equal(result, expected)
182+
183+
def test_constructor_from_dict_keys(self, constructor, using_infer_string):
184+
# https://github.com/pandas-dev/pandas/issues/60343
185+
d = {"a": 1, "b": 2}
186+
result = constructor(d.keys(), dtype="str")
187+
if using_infer_string:
188+
assert result.dtype == "str"
189+
else:
190+
assert result.dtype == "object"
191+
expected = constructor(list(d.keys()), dtype="str")
192+
tm.assert_equal(result, expected)

pandas/tests/io/test_fsspec.py

-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ def test_arrowparquet_options(fsspectest):
209209
assert fsspectest.test[0] == "parquet_read"
210210

211211

212-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string) fastparquet")
213212
def test_fastparquet_options(fsspectest):
214213
"""Regression test for writing to a not-yet-existent GCS Parquet file."""
215214
pytest.importorskip("fastparquet")

0 commit comments

Comments
 (0)