Skip to content
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

DatetimeIndex constructor should accept set #882

Closed
amgcc opened this issue Feb 27, 2024 · 3 comments
Closed

DatetimeIndex constructor should accept set #882

amgcc opened this issue Feb 27, 2024 · 3 comments

Comments

@amgcc
Copy link
Contributor

amgcc commented Feb 27, 2024

Describe the bug
Providing a set to the DatetimeIndex constructor is rejected.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
import pandas as pd


def main() -> None:
    s = {pd.Timestamp(2020, 1, 1), pd.Timestamp(2021, 1, 2)}
    l = [pd.Timestamp(2020, 1, 1), pd.Timestamp(2021, 1, 2)]
    idx = pd.DatetimeIndex(s)
    print(idx)

    idx2 = pd.DatetimeIndex(l)
    print(idx2)


if __name__ == '__main__':
    main()
  1. Indicate which type checker you are using (mypy or pyright).
    mypy
  2. Show the error message received from that type checker while checking your example.
dtidx_ctor.py:7: error: Argument 1 to "DatetimeIndex" has incompatible type "set[Timestamp]"; expected "ExtensionArray | ndarray[Any, Any] | Index[Any] | Series[Any] | ndarray[Any, Any] | list[Any] | tuple[Any, ...]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:

  • OS: [e.g. Windows, Linux, MacOS] Linux
  • OS Version [e.g. 22] Fedora 7
  • python version 3.11
  • version of type checker 1.8
  • version of installed pandas-stubs 2.1.4
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 27, 2024

There's a related open issue on pandas about allowing sets as arguments for indexes:
pandas-dev/pandas#55425

The problem here is that an Index is ordered, so providing a set doesn't allow the order to be determined. Here's an example with pd.Index().

>>> pd.Index(set([1,2]))
Index([1, 2], dtype='int64')
>>> pd.Index(set([2,1]))
Index([1, 2], dtype='int64')

We decided for the stubs to disallow sets because the behavior for pandas is ambiguous.

@amgcc
Copy link
Contributor Author

amgcc commented Feb 27, 2024

We often use DatetimeIndex in our application for union operations between calendars. In this case, the ordering isn't important. I think there is nothing "wrong" about using a set to initialize an index. Please reconsider.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 28, 2024

We often use DatetimeIndex in our application for union operations between calendars. In this case, the ordering isn't important. I think there is nothing "wrong" about using a set to initialize an index. Please reconsider.

We discussed this in the pandas dev meeting and the intent is to (eventually) disallow sets with Index constructors. The recommendation for your code is to listify the arguments to the constructor, e.g.,

import pandas as pd


def main() -> None:
    s = {pd.Timestamp(2020, 1, 1), pd.Timestamp(2021, 1, 2)}
    l = [pd.Timestamp(2020, 1, 1), pd.Timestamp(2021, 1, 2)]
    idx = pd.DatetimeIndex(list(s))
    print(idx)

    idx2 = pd.DatetimeIndex(l)
    print(idx2)

@Dr-Irv Dr-Irv closed this as completed Feb 28, 2024
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

No branches or pull requests

2 participants