Skip to content

Rewrite dict literal for files in pandas/tests/frame/test_indexing and test_where #38226

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 3 commits into from

Conversation

Qbiwan
Copy link
Contributor

@Qbiwan Qbiwan commented Dec 2, 2020

@Qbiwan
Copy link
Contributor Author

Qbiwan commented Dec 2, 2020

@jreback checks showing some issues: pandas/tests/frame/indexing/test_indexing.py:77: error: "Set[Iterator[Tuple[Any, int]]]" has no attribute "keys" [attr-defined]

maybe its because replacing dict() with {} in this situation would cause python to mistake {} to be set(). Should we leave it as dict() for this case?

l=[1,2,3]
a = dict(zip(l, range(len(l))))
type(a)
>> dict
b = {zip(l, range(len(l)))}; 
type(b)
>> set

The second change, which is for pandas/tests/frame/test_where, might also show a problem as using dict() gives us a dictionary of three items correctly

df = {1:2,2:3,3:4}
dict((c, s + 1)  for c, s in df.items())
> {1: 3, 2: 4, 3: 5}

but using {} gives us a set of 3 tuples, which might not be what we intended

{(c, s + 1)  for c, s in df.items()}
> {(1, 3), (2, 4), (3, 5)}

@fangchenli
Copy link
Member

You could use dict comprehensions.

l = [1,2,3]
x = {value: index for index, value in enumerate(l)}
x 
>> {1: 0, 2: 1, 3: 2}

@Qbiwan Qbiwan closed this Dec 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLN: C408 Unnecessary dict call - rewrite as a literal
2 participants