FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ENH]: Support `pandas.NA` · Issue #22126 · matplotlib/matplotlib · GitHub

Repository navigation

[ENH]: Support pandas.NA #22126

Open
Open

Description

Problem

Since a while now, pandas is moving towards better support for data with missing values: https://pandas.pydata.org/pandas-docs/stable/user_guide/missing_data.html

In particular, they introduced nullable data types:

which are all based on pandas.NA. Because missing values are becoming more and more important to handle, we see other projects supporting this feature as well, for example

Proposed solution

Matplotlib should detect pandas.NA and treat it like a missing value. (e.g. like numpy.nan)

Activity

  1. jklymak commented on Jan 6, 2022

    Member

    What does to_numpy or .values do? We don't directly do anything with pandas data types except immediately turn them into numpy arrays.

    If numpy starts supporting other invalid flags we can discuss what to do (probably just add a bunch of redundant checks) but from Matlotlibs point of view I'm not sure there is an action here?

  2. oscargus commented on Apr 10, 2022

    Member

    It seems like to_numpy is not converting the NA values. Conditionally if dtype=object.

    In [5]: df
    Out[5]: 
       col1  col2
    0     1     3
    1     2  <NA>
    
    In [6]: df.to_numpy()
    Out[6]: 
    array([[1, 3],
           [2, <NA>]], dtype=object)
    
  3. oscargus commented on Apr 10, 2022

    Member

    Pandas has a keyword argument na_value that can be use to replace the NA values during conversion. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_numpy.html

    However, xarray does not.

  4. github-actions commented on Dec 1, 2025

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  5. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jan 1, 2026
  6. timhoffm commented on Jan 2, 2026

    Member

    We should handle pd.NA (or explicitly decide that we can't do this).

    At least for numeric data, I suspect that we could convert pd.NA to NaN.

  7. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jan 2, 2026
  8. jklymak commented on Jan 2, 2026

    Member

    We don't include pandas as a dependency (except for tests).

    I'm not sure what we are supposed to do here - if to_numpy just returns an object array with pandas objects in it, then we can't deal with it. I think this is a pandas (and perhaps numpy issue). Numpy has formally handled identifying missing data using masked arrays. I don't think masked arrays have proven very popular, and, practically, most of the world is fine with NaN as a bad data sentinel. Given this issue is 4 years old, I'm guessing pd.NA has suffered a similar lack of use.

  9. timhoffm commented on Jan 2, 2026

    Member

    What currently works and what doesn't

    In order to carry pd.NA (and thus be affected by it) the pandas data types must be specific nullable pandas datatype. I've performed checks like

    In [5]: pd.Series([1, pd.NA,  3], dtype="Float64").to_numpy().dtype
    Out[5]: dtype('float64')
    pandas nullable dtype to_numpy().dtype
    Int64 float64
    Float32 float32
    Float64 float64
    boolean object

    So the numeric types are converted to floats and pd.NA is represented as NaN. The boolean type is converted to object. This behavior with default to_numpy() brings compatibility with matplotlib as good as we can get. For visualization it is typically irrelevant that Int64 is converted to float. Bool values should be quite rare for visualization and there is no native numpy type that can reasonably represent NA (converting bool to float would be a bit stretched to far). So for now, there is nothing to do.

    What changes with pandas 3

    However, it seems that pandas 3 changes the above. https://pandas.pydata.org/docs/dev/whatsnew/v3.0.0.html#changed-treatment-of-nan-values-in-pyarrow-and-numpy-nullable-floating-dtypes

    With "future.distinguish_nan_and_na" enabled, ser.to_numpy() (and frame.values and np.asarray(obj)) will convert to object dtype if NA entries are present, where before they would coerce to NaN. To retain a float numpy dtype, explicitly pass na_value=np.nan to Series.to_numpy().

    So we may consider switching to to_numpy(na_value=np.nan). Otherwise, we loose support for Int64, Float32 and Float64.

    Note: We don't need to import pandas to support this. It is sufficient to inspect whether the type is a pandas type or try...except with the na_value kwarg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL