Skip to content

ENH: Expose pandas._libs.missing.NAType #48388

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
1 of 3 tasks
randolf-scholz opened this issue Sep 4, 2022 · 2 comments
Closed
1 of 3 tasks

ENH: Expose pandas._libs.missing.NAType #48388

randolf-scholz opened this issue Sep 4, 2022 · 2 comments
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member Typing type annotations, mypy/pyright type checking

Comments

@randolf-scholz
Copy link
Contributor

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When typing hinting using pandas-stubs, using type[pd.NA] is not possible.

error: Variable "pandas._libs.missing.NA" is not valid as a type  [valid-type]

Instead, it needs to be annotated as NAType which is currently hidden in the private library pandas._libs.missing.

Feature Description

Make NAType accessible through from pandas import NAType.

Alternative Solutions

Rework how padnas.NA works such that type[pandas.NA] is a valid type hint.

Additional Context

Example

import pandas as pd

def first_negative(s: Series[float]) -> float | type[pd.NA]:
    """return the first negative number in s or pd.NA""""
    negs = s[s<0]
    return negs[0] if len(negs) else pd.NA
@randolf-scholz randolf-scholz added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2022
@twoertwein
Copy link
Member

twoertwein commented Sep 4, 2022

I belief/hope that def first_negative(s: Series[float]) -> float | Literal[pd.NA]: ... at least works with 1.5.rc0?

edit: That doesn't work.

@twoertwein twoertwein added the Typing type annotations, mypy/pyright type checking label Sep 4, 2022
@twoertwein
Copy link
Member

In the next major pandas release you will be able to do the following:

import pandas as pd
from pandas.api.typing import NAType

def na() -> NAType:
    return pd.NA

xref #48577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

No branches or pull requests

2 participants