Skip to content

REGR: unstack on 'int' dtype prevent fillna to work #37115

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

Closed
2 of 3 tasks
ant1j opened this issue Oct 14, 2020 · 4 comments · Fixed by #38244
Closed
2 of 3 tasks

REGR: unstack on 'int' dtype prevent fillna to work #37115

ant1j opened this issue Oct 14, 2020 · 4 comments · Fixed by #38244
Labels
Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ant1j
Copy link

ant1j commented Oct 14, 2020

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd
import numpy as np

df1 = pd.DataFrame({
    'a': ['A',  'A',  'B'], 
    'b': ['ca', 'cb', 'cb'],
    'v': [10] * 3,
})

df1 = df1.set_index(['a', 'b'])

# int column
df1['is_'] = 1

df1 = df1.unstack('b')

# Will not work, keeping NaN in the value
df1[('is_', 'ca')] = df1[('is_', 'ca')].fillna(0)

# Will raise ValueError: Cannot convert non-finite values (NA or inf) to integer
df1[('is_', 'ca')] = df1[('is_', 'ca')].astype('uint8')

Problem description

df.unstack is creating columns from a int column, but adds some NaNs as expected.

Trying to get rid of the NaN is not possible as fillna will silently fail and leave the column values as is.

Potential Source of the Problem

It looks like with this configuration, the Block for this df1[('is_', 'ca')] Series in the BlockManager is actually IntBlock, but holds a float dtype np.ndarray (!?).

From what I understand, as soon as the BlockManager operates a block consolidation (with the _consolidate function), the Block is converted to a FloatBlock and everything is OK.
e.g. a call to cat2._is_mixed_type will trigger a _consolidate and thus clear the issue.

Potential Solution Suggestion

Should the unstack function fires a consolidation of blocks, especially for column types that do not accept NaN values (like 'int' and 'bool' from numpy)?

Some potential workarounds identified so far

  • Trigger a Block consolidate:
    • Add a column: the bug appeared if I use fillna before adding a column, but not after
    • Call cat2._is_mixed_type
    • Call df1.infos(): this was the most surprising one (it is because if triggers df.count() and thus consolidation)
  • Use a float value: df1['is_'] = 1.0

Expected Output

      v       is_
b    ca    cb  ca   cb
a
A  10.0  10.0   1  1.0
B   NaN  10.0   0  1.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : db08276
python : 3.8.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.cp1252

pandas : 1.1.3
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.16.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.4
pandas_gbq : None
pyarrow : 1.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@ant1j ant1j added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 14, 2020
@dsaxton
Copy link
Member

dsaxton commented Oct 15, 2020

This seems like a duplicate of #36495, also caused by #34389.

@simonjayhawkins has a PR #36668 that fixes this which I think could be reopened and merged now for 1.1.4.

@dsaxton dsaxton added Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 15, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.5 milestone Nov 25, 2020
@jreback jreback modified the milestones: 1.1.5, Contributions Welcome Nov 25, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Nov 25, 2020
@simonjayhawkins
Copy link
Member

This seems like a duplicate of #36495, also caused by #34389.

@dsaxton do you recall if you did a bisect on this? I'm getting...

first bad commit: [12f9a10] REF: unstack (#33474) cc @jbrockmendel

@dsaxton
Copy link
Member

dsaxton commented Nov 26, 2020

@simonjayhawkins I think I did do a bisect if I'm remembering correctly, but I would trust your analysis over mine

@simonjayhawkins
Copy link
Member

the code sample in the op still fails with #34407 which fixes #36495, so will assume these are unrelated.

@simonjayhawkins simonjayhawkins changed the title BUG: unstack on 'int' dtype prevent fillna to work REGR: unstack on 'int' dtype prevent fillna to work Dec 2, 2020
@simonjayhawkins simonjayhawkins modified the milestones: Contributions Welcome, 1.1.5 Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants