-
Notifications
You must be signed in to change notification settings - Fork 771
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
pandas DataFrame.index property has wrong return type #387
Comments
Also an issue with
|
Seems like the result should be a union of Index or MultiIndex in this case. You set the index to a multiindex so your case makes sense, but before your call to set_index it would have just been a regular Index, no? |
This one is going to be problematic. If we say it is an index, then we will flag an error when it is assigned to MultiIndex, but the same would happen if we use Union[Index, MultiIndex]. As MultiIndex is a subtype of Index, it seems like the right thing here is to return an Index type, unless this is always a multiindex which I don't believe it is. I don't see a good solution. |
I thought I tried a return type of |
So here is code that breaks if you type it as df = pd.DataFrame.from_records([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"])
dfmi = df.set_index(["a", "b"])
levels = df.index.levels In this case I think you're better off with In my example above, if you use
then pylance doesn't complain. On the other hand, if you knew your index was another subtype of |
MultiIndex is a subtype of Index: https://github.com/pandas-dev/pandas/blob/c33c3c03c227d53139d6b9a2b6bc1dc1f9400542/pandas/core/indexes/multi.py#L175 I think the correct thing here is to return Index. You can declare your variable as a MultiIndex and then use a # type: ignore comment on that line to silence the type check. |
Yes, I get that. The issue is that an operation such as If you end up releasing with the |
Alternatively you can use typing.cast, which is more portable. |
This issue has been fixed in version 2020.9.6, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202096-23-september-2020 |
Environment data
Expected behaviour
No error
Actual behaviour
Above reports
This seems to fix it:
The text was updated successfully, but these errors were encountered: