I have the following DataFrame that consists of integer values:
df = pd.DataFrame(data=1, columns=['a','b'], index=[1,2,3])
a b
1 1 1
2 1 1
3 1 1
I would like to set missing values on individual cells, and when I try:
df.at[1,'a'] = np.nan
Then I get this exception:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "pandas/core/indexing.py", line 2159, in __setitem__
self.obj._set_value(*key, takeable=self._takeable)
File "pandas/core/frame.py", line 2582, in _set_value
engine.set_value(series._values, index, value)
File "pandas/_libs/index.pyx", line 124, in pandas._libs.index.IndexEngine.set_value
File "pandas/_libs/index.pyx", line 133, in pandas._libs.index.IndexEngine.set_value
File "pandas/_libs/index.pyx", line 570, in pandas._libs.index.convert_scalar
ValueError: Cannot assign nan to integer series
It seems function DataFrame.at cannot casting integers to float, if set NaNs.
For me working DataFrame.loc:
df.loc[1,'a'] = np.nan
print (df)
a b
1 NaN 1
2 1.0 1
3 1.0 1
@Peter Leimbigler explanation:
The reason why any type-casting is needed in the first place is because nan is of type float, and the int data type has no support for nan or any other missing value. In order for a numeric column to contain nan, it must be of type float.
@pir explanation:
pandas.DataFrame.at is optimized for specific cell access. Therefore it cannot change the dtype of the column. However,
loccan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With