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

ENH: Enable fillna(value=None) #58085

Merged
merged 6 commits into from
Apr 15, 2024

Conversation

rhshadrach
Copy link
Member

@rhshadrach rhshadrach commented Mar 30, 2024

The one behavior introduced here I think is important to highlight is using None with non-object dtypes. In such a case we treat None as the NA value for the given dtype for NumPy float/datetime and pandas' EA dtypes. However for external EAs, by default we try to put None in the values and raise if this fails.

For object dtype, we replace any NA value with None.

cc @Dr-Irv

@rhshadrach rhshadrach added Enhancement Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Mar 30, 2024
@rhshadrach rhshadrach marked this pull request as ready for review March 31, 2024 17:59
@@ -6827,6 +6827,10 @@ def fillna(
reindex : Conform object to new index.
asfreq : Convert TimeSeries to specified frequency.

Notes
-----
For non-object dtype, ``value=None`` will use the NA value of the dtype.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments are all docs related.

Notes
-----
For non-object dtype, ``value=None`` will use the NA value of the dtype.

Examples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should add an example with value=None ?

Copy link
Member Author

@rhshadrach rhshadrach Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good suggestion, but what do you think about doing it in https://pandas.pydata.org/docs/user_guide/missing_data.html#filling-missing-data instead of the docstring here? Can then link to this in the docstring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good suggestion, but what do you think about doing it in https://pandas.pydata.org/docs/user_guide/missing_data.html#filling-missing-data instead of the docstring here? Can then link to this in the docstring.

That's fine, although I think the behavior of None with respect to how it is treated as a missing value isn't necessarily well explained on that page, so maybe that should be done as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docs:

image

image

@@ -35,7 +35,7 @@ Other enhancements
- Support passing a :class:`Series` input to :func:`json_normalize` that retains the :class:`Series` :class:`Index` (:issue:`51452`)
- Users can globally disable any ``PerformanceWarning`` by setting the option ``mode.performance_warnings`` to ``False`` (:issue:`56920`)
- :meth:`Styler.format_index_names` can now be used to format the index and column names (:issue:`48936` and :issue:`47489`)
-
- :meth:`DataFrame.fillna` and :meth:`Series.fillna` can now accept ``value=None``; for non-object dtype the corresponding NA value will be used (:issue:`57723`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other change here is that you must specify a value for value, whereas before it defaulted to None

Copy link
Member Author

@rhshadrach rhshadrach Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the default value of None would also raise. In other words, both before an after this PR you have to specify value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the default value of None would also raise. In other words, both before an after this PR you have to specify value.

That was true if you didn't specify method. But with 2.2:

>>> import pandas as pd
>>> import numpy as np
>>> s=pd.Series([1,np.nan,2])
>>> s.fillna(method="ffill")
<stdin>:1: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
0    1.0
1    1.0
2    2.0
dtype: float64
>>>

So I think it is worth saying that value is now required.

Copy link
Member Author

@rhshadrach rhshadrach Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But method is no longer an argument. In 2.x, you either had to specify value or method, otherwise we'd raise. Any code that does not raise a warning in 2.2 is already specifying the value argument. Any code that raises a warning in 2.2 will raise a TypeError in 3.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So don't we have to say somewhere that we are enforcing the deprecation that method is no longer an argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Removed deprecated keyword ``method`` on :meth:`Series.fillna`, :meth:`DataFrame.fillna` (:issue:`57760`)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. All good then

@mroeschke mroeschke added this to the 3.0 milestone Apr 15, 2024
@mroeschke mroeschke merged commit e7a96a4 into pandas-dev:main Apr 15, 2024
46 checks passed
@mroeschke
Copy link
Member

Thanks @rhshadrach

@rhshadrach rhshadrach deleted the enh_fillna_allow_none branch April 15, 2024 20:32
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
* ENH: Enable fillna(value=None)

* fixups

* fixup

* Improve docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: fillna() doesn't work with value=None without method specified, but method is deprecated
3 participants