Skip to content

Assigned conversion via loc to Int64 fails under peculiar conditions #31861

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
shippy opened this issue Feb 10, 2020 · 7 comments · Fixed by #41712
Closed

Assigned conversion via loc to Int64 fails under peculiar conditions #31861

shippy opened this issue Feb 10, 2020 · 7 comments · Fixed by #41712
Assignees
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@shippy
Copy link

shippy commented Feb 10, 2020

Code Sample, a copy-pastable example if possible

test = pd.DataFrame({'a': ['test'], 'b': [np.nan]})
test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
# *** AttributeError: 'IntegerArray' object has no attribute 'size'

Problem description

Assignment of .astype('Int64') sometimes fails with AttributeError: 'IntegerArray' object has no attribute 'size'.

Initially, I thought this was a regression from #25584, but it only manifests under very specific conditions:

  1. There is exactly one null value in the converted column
    # works
    test = pd.DataFrame({'a': ['test', 'test'], 'b': [np.nan, 1.0]})
    test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
    
    # works
    test = pd.DataFrame({'a': ['test', 'test'], 'b': [np.nan, np.nan]})
    test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
    
    # fails
    test = pd.DataFrame({'a': ['test'], 'b': [np.nan]})
    test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
    # *** AttributeError: 'IntegerArray' object has no attribute 'size'
  2. There is another column in the DataFrame
    # works
    test = pd.DataFrame({'b': [np.nan]})
    test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
  3. Assignment happens via df.loc[:, colname] - without assignment, or with assignment to df[colname], things work.
    test = pd.DataFrame({'a': ['test'], 'b': [np.nan]})
    
    # fails
    test.loc[:, 'b'] = test['b'].astype('Int64')
    # *** AttributeError: 'IntegerArray' object has no attribute 'size'
    
    # fails
    test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')
    # *** AttributeError: 'IntegerArray' object has no attribute 'size'
    
    # works
    test.loc[:, 'b'].astype('Int64')
    
    # works
    test['b'] = test['b'].astype('Int64')

Expected Output

test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64') shouldn't raise an AttributeError.

print(test) after assignment should read along the lines of

0    <NA>
dtype: Int64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-46-generic
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.1
numpy : 1.16.1
pytz : 2018.9
dateutil : 2.8.0
pip : 19.3.1
setuptools : 41.4.0
Cython : None
pytest : 3.10.1
hypothesis : None
sphinx : 1.8.5
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.3.0
html5lib : None
pymysql : 0.9.3
psycopg2 : None
jinja2 : 2.10
IPython : 7.2.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.3.0
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 3.10.1
pyxlsb : None
s3fs : None
scipy : 1.2.0
sqlalchemy : 1.2.17
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@shippy
Copy link
Author

shippy commented Feb 10, 2020

I'm happy to report that this works when using pd.NA instead of np.nan:

# works
test = pd.DataFrame({'a': ['test'], 'b': [pd.NA]})
test.loc[:, 'b'] = test.loc[:, 'b'].astype('Int64')

@Dr-Irv Dr-Irv added ExtensionArray Extending pandas with custom dtypes or arrays. NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions labels Sep 12, 2020
@Dr-Irv
Copy link
Contributor

Dr-Irv commented Sep 12, 2020

This works fine in pandas 1.1.1.

A PR that adds a test for this would be welcome.

@TAJD
Copy link
Contributor

TAJD commented Sep 13, 2020

take

@hardikpnsp
Copy link
Contributor

@TAJD, are you still working on the test? I would like to contribute if otherwise.

@TAJD
Copy link
Contributor

TAJD commented Oct 3, 2020

Please contribute! I'm no longer working on this.

@hardikpnsp
Copy link
Contributor

Take

@hardikpnsp
Copy link
Contributor

So far I have added a test for functionality similar to the one provided in the issue. Basically, test astype via loc on np.nan on Int64, Int32 and Int16. should I add tests for pd.NA too? Was adding a test for Int32 and Int16 unnecessary? Also, do we need similar tests for Series? As I am quite new to this, I would appreciate some guidance.

hardikpnsp added a commit to hardikpnsp/pandas that referenced this issue Oct 7, 2020
@jreback jreback added this to the 1.2 milestone Oct 10, 2020
@hardikpnsp hardikpnsp removed their assignment Oct 27, 2020
@jreback jreback modified the milestones: 1.2, Contributions Welcome Nov 25, 2020
@mroeschke mroeschke mentioned this issue May 29, 2021
15 tasks
@mroeschke mroeschke modified the milestones: Contributions Welcome, 1.3 May 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExtensionArray Extending pandas with custom dtypes or arrays. good first issue NA - MaskedArrays Related to pd.NA and nullable extension arrays Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants