Bug Fix: #60343 Construction of Series / Index fails from dict keys when "str" dtype is specified explicitly
State: closed

- This PR fixes #60343 @jorisvandenbossche 
The default behaviour (pd.Index(d.keys())) worked correctly, but explicitly setting dtype="str" raised a ValueError. The issue stemmed from dict_keys not being converted to a proper array-like structure before being passed to StringDtype, which couldn't handle such inputs. 

To fix the issue:

- KeyView was introduced to identify and preprocess dict_keys before passing them to Pandas internals. The keys are now converted to a list for compatibility.
- Updated logic in Index and sanitize_array to map dtype="str" to StringDtype(storage="python"). Updated check_array_indexer to allow empty boolean indexers for StringArray
- New test added "test_index_from_dict_keys_with_dtype" to ensure: 
Default inference (pd.Index(d.keys())) works.
Explicit dtype="str" works, resulting in string[python].

- Updated existing tests (test_is_object and test_empty_fancy) to handle new behaviours introduced by the fix.

After the fix both the default (pd.Index(d.keys())) and explicit (pd.Index(d.keys(), dtype="str")) cases work:
<img width="870" alt="Screenshot 2024-11-19 at 3 17 08 AM" src="https://github.com/user-attachments/assets/00346320-52c4-4249-9572-b16a63a1711d">


Labels:
name: Strings
description: String extension data type and string data
name: Constructors
description: Series/DataFrame/Index/pd.array Constructors

Comments:
I was able to fix the bug but I am a bit stuck on how to fix the checks. Could @jorisvandenbossche or anyone else help? Especially the unit tests. I tried to fix the pre-commit (using ruff lint fix) but every time I fixed a formatting issue, after running pre-commit it goes to the initial position before I did the fix. 

For the Doc build and upload check (it was giving an error for every declaration of ipython that didn't have import pandas as pd), I manually inserted it but don't know if there is an easy way.
> For the Doc build and upload check (it was giving an error for every declaration of ipython that didn't have import pandas as pd), I manually inserted it but don't know if there is an easy way.

That should normally not have been needed. Did you get those errors locally? (in that case maybe something with the set up was wrong) 
On the CI build I see that there is an error specifically in the `doc/source/getting_started/comparison/includes/nth_word.rst` file.

> > For the Doc build and upload check (it was giving an error for every declaration of ipython that didn't have import pandas as pd), I manually inserted it but don't know if there is an easy way.
> 
> That should normally not have been needed. Did you get those errors locally? (in that case maybe something with the set up was wrong) On the CI build I see that there is an error specifically in the `doc/source/getting_started/comparison/includes/nth_word.rst` file.

Yes when I was doing pre-commit hook locally, I saw that without ipython import it was causing failed hooks in each.rst file 
Hi Joris! I will make another pull request. I wanted to work on a new branch and accidentally removed my changes.