Skip to content

BUG: .at/.iat and Series.__setitem__ do not upcast int to float #20643

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
jschendel opened this issue Apr 10, 2018 · 5 comments · Fixed by #45154
Closed

BUG: .at/.iat and Series.__setitem__ do not upcast int to float #20643

jschendel opened this issue Apr 10, 2018 · 5 comments · Fixed by #45154
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@jschendel
Copy link
Member

Code Sample, a copy-pastable example if possible

Series.at doesn't upcast to float:

In [2]: s = pd.Series([0, 1, 2], index=list('abc'))

In [3]: s.at['b'] = 2.7

In [4]: s
Out[4]:
a    0
b    2
c    2
dtype: int64

Series.iat doesn't upcast to float:

In [5]: s = pd.Series([0, 1, 2], index=list('abc'))

In [6]: s.iat[1] = 3.1

In [7]: s
Out[7]:
a    0
b    3
c    2
dtype: int64

Series.__setitem__ doesn't upcast to float:

In [8]: s = pd.Series([0, 1, 2], index=list('abc'))

In [9]: s[1] = 5.5

In [10]: s
Out[10]:
a    0
b    5
c    2
dtype: int64

Same story for DataFrame.at and DataFrame.iat:

In [11]: df = pd.DataFrame({'A': [0, 1, 2]}, index=list('abc'))

In [12]: df.at['b', 'A'] = 3.14

In [13]: df
Out[13]:
   A
a  0
b  3
c  2

In [14]: df.iat[1, 0] = 7.77

In [15]: df
Out[15]:
   A
a  0
b  7
c  2

Note that loc and iloc do upcast to float for both Series and DataFrame:

In [16]: s = pd.Series([0, 1, 2], index=list('abc'))

In [17]: s.loc['b'] = 2.7

In [18]: s
Out[18]:
a    0.0
b    2.7
c    2.0
dtype: float64

In [19]: df = pd.DataFrame({'A': [0, 1, 2]}, index=list('abc'))

In [20]: df.iloc[0, 0] = 5.5

In [21]: df
Out[21]:
     A
a  5.5
b  1.0
c  2.0

Problem description

Series.at, Series.iat, Series.__setitem__, DataFrame.at, and DataFrame.iat do not upcast integer data to float.

Expected Output

I'd expect the values of the Series/DataFrame to be upcast to float in all scenarios mentioned.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: e8f206d
python: 3.6.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.0.dev0+735.ge8f206d
pytest: 3.1.2
pip: 9.0.1
setuptools: 39.0.1
Cython: 0.25.2
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.6.0
xarray: 0.9.6
IPython: 6.1.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: None
html5lib: 0.999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: 0.1.0
pandas_gbq: None
pandas_datareader: None

@gfyoung gfyoung added Dtype Conversions Unexpected or buggy dtype conversions Compat pandas objects compatability with Numpy or Python functions labels Apr 10, 2018
@gfyoung
Copy link
Member

gfyoung commented Apr 10, 2018

Hmm...that does seem a little odd. We should have consistency.

@srinivasreddy
Copy link
Contributor

i would like to take this.

@gfyoung
Copy link
Member

gfyoung commented Apr 10, 2018

Go for it!

@jimmywan
Copy link

Note that this also affects what happens when you try to set a value to None:

>>> s = pd.Series([0, 1, 2], index=list('abc'))
>>>  s.iat[0] = 5
>>> s
a    5
b    1
c    2
dtype: int64

Setting a value to None via .iat fails because None is not a valid value for an int64 and it won't upcast.

>>> s.iat[0] = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MYINSTALLDIR/site-packages/pandas/core/indexing.py", line 1886, in __setitem__
    self.obj._set_value(*key, takeable=self._takeable)
  File "MYINSTALLDIR/site-packages/pandas/core/series.py", line 965, in _set_value
    self._values[label] = value
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Setitng a value to None via .iloc succeeds because it will upcast to float64.

>>> s.iloc[0] = None
>>> s
a    NaN
b    1.0
c    2.0
dtype: float64

@mroeschke mroeschke added the Indexing Related to indexing on series/frames, not to indexes themselves label Oct 27, 2019
@mroeschke mroeschke added Bug and removed Compat pandas objects compatability with Numpy or Python functions labels Apr 10, 2020
@jbrockmendel
Copy link
Member

This looks like it is working now, could use test

@jbrockmendel jbrockmendel added the Needs Tests Unit test(s) needed to prevent regressions label Jun 10, 2021
@mroeschke mroeschke removed Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 19, 2021
@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Dec 21, 2021
@jreback jreback added this to the 1.4 milestone Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
8 participants