Skip to content

Commit bb41546

Browse files
[backport 2.3.x] BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly (#60436) (#60793)
BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly (#60436) Co-authored-by: Joris Van den Bossche <[email protected]> (cherry picked from commit 84bf1ef) Co-authored-by: tasfia8 <[email protected]>
1 parent 6451f79 commit bb41546

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
@@ -589,6 +589,8 @@ def sanitize_array(
589589
# create an extension array from its dtype
590590
_sanitize_non_ordered(data)
591591
cls = dtype.construct_array_type()
592+
if not hasattr(data, "__array__"):
593+
data = list(data)
592594
subarr = cls._from_sequence(data, dtype=dtype, copy=copy)
593595

594596
# GH#846

pandas/tests/base/test_constructors.py

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

pandas/tests/io/test_fsspec.py

-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ def test_arrowparquet_options(fsspectest):
197197

198198

199199
@td.skip_array_manager_not_yet_implemented # TODO(ArrayManager) fastparquet
200-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string) fastparquet")
201200
def test_fastparquet_options(fsspectest):
202201
"""Regression test for writing to a not-yet-existent GCS Parquet file."""
203202
pytest.importorskip("fastparquet")

0 commit comments

Comments
 (0)