Skip to content

BUG: reindex (and atleast several other methods) do not respect fill_value=None #61291

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

Open
3 tasks done
TimAllen576 opened this issue Apr 15, 2025 · 1 comment
Open
3 tasks done
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Needs Discussion Requires discussion from core team before further action PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint

Comments

@TimAllen576
Copy link

TimAllen576 commented Apr 15, 2025

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd


df = pd.DataFrame({'1'})
new_df = df.reindex(range(2), fill_value=None)
print(new_df,'\n')

data = [None, complex(1)]
arr2 = pd.core.construction.array(data)
indexer = [0, -1]
new_arr = arr2.take(indexer, allow_fill=True, fill_value=None)
print(arr2)
print(new_arr)

Issue Description

User-facing bug placing NaN into df instead of None and the underlying ExtensionArray.take method (with a more contrived example to ensure the types are valid) which is a significant cause of the behaviour.

This is less of a bug and more a discussion on default behaviour in pandas. It is impossible to "fill" a value of None in an already existing dataframe/array as far as I can see in pandas, although None is often the default value used to fill during dataframe creation1. The reason for this behaviour seems to run very deeply and shows active choices by implementors to have this behaviour, see: example with arr.take (with an implementors note which I did not completely understand), also BaseArrayManager._make_na_array and the optimised code for NDFrame._reindex_multi.
This problem seems to have been almost seen but brushed past in #20640 where fill_value is recognised to not always be honored due to compatibility with the underlying arrays. However, as shown there are also cases where None is a valid entry in the array which are blanket ignored (even though I'd argue that the array type should possibly be adjusted to fit the fill value). Some complexity also comes from there not being a None in C however I strongly dislike this argument as there seems to be standins/a way to work with them throughout the rest of the library. Note I have not dug deeply into the C/Cython code myself.

In closing, I think None should be able to be used as a fill_value when reindexing but it is currently intentionally and unecessarily mangled into np.nan at many levels. Discussion is strongly encouraged as any actual changes WOULD subtly change existing behaviour and at some point I just want to understand why these choices were made.

Expected Behavior


0
0 1
1 None

<NumpyExtensionArray>
[None, (1+0j)]
Length: 2, dtype: object
<NumpyExtensionArray>
[None, None]
Length: 2, dtype: object

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.9
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.26100
machine : AMD64
processor : AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_New Zealand.1252
pandas : 2.2.3
numpy : 2.1.0
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

Footnotes

  1. Which is where I ran into this problem, creating a dataframe of strings with holes, then trying to consistently reshape it. Of course there are plenty of ways for me to circumvent this problem but I want to demonstrate this is not arbitrary/irrelevant.

@TimAllen576 TimAllen576 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 15, 2025
@rhshadrach
Copy link
Member

rhshadrach commented Apr 16, 2025

Thanks for the report. I expect this to be a part of PDEP-16 when the proposal is finished. Namely, I think with switching over to pd.NA as the NA value across dtypes, we can then treat None as just the Python object it is and not an NA value for non-object dtypes (whether it will be regarded as an NA value in object dtype seems less clear to me). However the core team is reluctant on making any changes without the full proposal.

Looking over issues tagged with PDEP-16, I'm not seeing any mentioning None behavior so leaving this open.

@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Needs Discussion Requires discussion from core team before further action PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Needs Discussion Requires discussion from core team before further action PDEP missing values Issues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint
Projects
None yet
Development

No branches or pull requests

2 participants