Skip to content

BUG: when re-index is filling values for both index and columns with dtype uint8 the filled values are incorrect #48184

Closed
@KerimovEmil

Description

@KerimovEmil

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

# this example works correctly, showing that the bug is not on all dtypes
df = pd.DataFrame([[1,2], [3, 4]], index=list('ab'), columns=list('qw'), dtype='int64')
df.reindex(index=list('abcd'), columns=list('qwer'), fill_value=10)

# this example has the bug
df = pd.DataFrame([[1,2], [3, 4]], index=list('ab'), columns=list('qw'), dtype='uint8')
df.reindex(index=list('abcd'), columns=list('qwer'), fill_value=10)

# this example works correctly, showing that it is only a bug when both index and columns are specified
df = pd.DataFrame([[1,2], [3, 4]], index=list('ab'), columns=list('qw'), dtype='uint8')
df.reindex(index=list('abcd'), fill_value=10).reindex(columns=list('qwer'), fill_value=10)

Issue Description

When using the fill_value option in the reindex method, when both index and columns are specified and the dtype is uint8 (note I did not try to find all dtypes that have this error), the NaNs are not being replaced by the fill_value, instead they are broadcasting the last item in the dataframe.

Default case that works as expected when using dtype='int64'
image

The bug when using dtype='uint8' and how the bug goes away when not passing in the index and columns at the same time.
image

Expected Behavior

df = pd.DataFrame([[1,2], [3, 4]], index=list('ab'), columns=list('qw'), dtype='uint8')
df_new = df.reindex(index=list('abcd'), fill_value=10).reindex(columns=list('qwer'), fill_value=10)

df_expected = pd.DataFrame([[1,2, 10, 10], [3, 4, 10, 10], [10, 10, 10, 10], [10, 10, 10, 10]], index=list('abcd'), columns=list('qwer'), dtype='uint8')
assert df_new.equals(df_expected)

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.9.12.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Canada.1252
pandas : 1.4.3
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions