BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly
State: closed

- This PR fixes #60343 @jorisvandenbossche and is a continuation of my earlier PR https://github.com/pandas-dev/pandas/pull/60383.

The default behavior (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.
-Handles Existing Test Cases and fixes previous PR fix issues.
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:
@jorisvandenbossche Can you check now please and if possible merge? I considered your comments in this updated PR and added the generic if.  Now besides the dict.keys(), it will handle other iterable such as dict.values() and addressing other similar types. 

My IDE is throwing some unintentional formatting issues which I can't seem to undo, but in this commit, I only changed the if condition in the construction.py and nothing else
This pull request is stale because it has been open for thirty days with no activity. Please [update](https://pandas.pydata.org/pandas-docs/stable/development/contributing.html#updating-your-pull-request) and respond to this comment if you're still interested in working on this.
Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen.
@tasfia8 apologies for the flow response. Your main commit https://github.com/pandas-dev/pandas/pull/60436/commits/eb11a270f61eaccde0dc2f43e0daf158af67d2a3 looks good, but there are still a bunch of unrelated changes. I will push a change to this branch to clean that up. 

In addition, we also still need to add a test.
@tasfia8 thanks for the initial contribution!
Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

1. Checkout backport branch and update it.

```
git checkout 2.3.x
git pull
```

2. Cherry pick the first parent branch of the this PR on top of the older branch:
```
git cherry-pick -x -m1 84bf1ef82912ebf497a304b0ffd90914bfc41ea9
```

3. You will likely have some merge/cherry-pick conflict here, fix them and commit:

```
git commit -am 'Backport PR #60436: BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly'
```

4. Push to a named branch:

```
git push YOURFORK 2.3.x:auto-backport-of-pr-60436-on-2.3.x
```

5. Create a PR against branch 2.3.x, I would have named this PR:

> "Backport PR #60436 on branch 2.3.x (BUG: fix construction of Series / Index from dict keys when "str" dtype is specified explicitly)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the `Still Needs Manual Backport` label once the PR gets merged.

If these instructions are inaccurate, feel free to [suggest an improvement](https://github.com/MeeseeksBox/MeeseeksDev).
                
Manual backport -> https://github.com/pandas-dev/pandas/pull/60793