Skip to content

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

Open
@TimAllen576

Description

@TimAllen576

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNeeds DiscussionRequires discussion from core team before further actionPDEP missing valuesIssues that would be addressed by the Ice Cream Agreement from the Aug 2023 sprint

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions