Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 0 commits into from

Conversation

tasfia8
Copy link
Contributor

@tasfia8 tasfia8 commented Nov 21, 2024

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:
Screenshot 2024-11-19 at 3 17 08 AM

@tasfia8
Copy link
Contributor Author

tasfia8 commented Nov 21, 2024

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.

@jorisvandenbossche jorisvandenbossche added this to the 2.3 milestone Nov 23, 2024
@jorisvandenbossche jorisvandenbossche added Strings String extension data type and string data Constructors Series/DataFrame/Index/pd.array Constructors labels Nov 23, 2024
@jorisvandenbossche
Copy link
Member

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.

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

I added a few comments. It seems you have made more changes than I think would be needed to fix it. I would try to focus the PR a bit more (also, only fixing either Index or Series constructor would also be fine)

return (
isinstance(arr_or_dtype, np.dtype)
and arr_or_dtype == "object"
or isinstance(arr_or_dtype, StringDtype)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to change the meaning of is_object_dtype to also include StringDtype. What was the reason you needed this change?

Copy link
Contributor Author

@tasfia8 tasfia8 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was included to pass the test case "test_is_object[string-True]" because it did not recognize the string as a valid object type. It passed the 60343 test case I added but failed the existing ones. In that case, would I leave the existing test case as is with no modification even if it fails so that I an make the PR more focused?

The main issue I am having is changing something for this bug is leaded to existing test cases and I am wondering if I should only be passing the test case I added for this bug or all of the test cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that test_is_object should change because of the fix that this PR is trying to do. The test already has a if using_infer_string and index.dtype == "string" check to change the expected result when using string dtype

Comment on lines 163 to 165
df = DataFrame(
{
"A": range(3),
"B": range(3),
"C": range(3)
}
).rename(columns={"B": "A"})
df = DataFrame({"A": range(3), "B": range(3), "C": range(3)}).rename(
columns={"B": "A"}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you included some unintended formatting changes. Maybe some setting in the IDE you are using that is conflicting with the formatting defaults in pandas?
I would recommend you to set up the pre-commit hook, which will ensure the code is formatted correctly when committing (see https://pandas.pydata.org/docs/development/contributing_codebase.html#pre-commit)

@tasfia8
Copy link
Contributor Author

tasfia8 commented Nov 26, 2024

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

@tasfia8
Copy link
Contributor Author

tasfia8 commented Nov 28, 2024

Hi Joris! I will make another pull request. I wanted to work on a new branch and accidentally removed my changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG (string): contruction of Series / Index fails from dict keys when "str" dtype is specified explicitly
2 participants