BUG: uint16 inserted as int16 when assigning row with dict · Issue #47294 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd import numpy as np

df = pd.DataFrame(columns=["actual", "reference"]) df.loc[0] = {'actual': np.uint16(40_000), 'reference': "nope"} df

actual reference

0 -25536 nope

df.info()

<class 'pandas.core.frame.DataFrame'> Int64Index: 1 entries, 0 to 0 Data columns (total 2 columns):

Column Non-Null Count Dtype


0 actual 1 non-null int16 1 reference 1 non-null object dtypes: int16(1), object(1)

Issue Description

Inserting a row with a dict, uint16 values are converted to int16 and the value conversion does not preserve the correct value. This also happens when assigning into an existing object-typed column (the conversion sequence seems to be -> int16 -> int in that case).

Expected Behavior

It's expected the dtype is preserved - uint16 if possible, or an int which is large enough to represent the value.

Installed Versions

python           : 3.8.10.final.0
python-bits      : 64
OS               : Linux
machine          : x86_64

pandas           : 1.4.2
numpy            : 1.22.4
pytz             : 2022.1
dateutil         : 2.8.2
pip              : 20.0.2
setuptools       : 44.0.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
markupsafe       : 2.1.1
matplotlib       : None
numba            : None
numexpr          : None
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