BUG: Pandas does not recognise older missing value code for double when reading Stata files prior to 108 (Stata 6) format · Issue #58149 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

The problem occurs when reading an external file, so unfortunately this

requires a data file linked to in the issue description

import pandas as pd df = pd.read_stata("aidsdata.dta")

Issue Description

Stata format 108 (corresponding to Stata 6) changed the code used to signify a missing value of type double (from 2^333 [1] to 2^1023 [2]). Pandas does not currently recognise this when reading these Stata file versions and therefore leaves the raw value in the DataFrame (note that the missing code for float types has not changed and therefore this works as expected):

df = pd.read_stata("aidsdata.dta") df[['gini', 'urbmf', 'miltpop', 'gaplit']][0:9] gini urbmf miltpop gaplit 0 3.900000e-01 1.028946e+00 10.668921 26.999992 1 1.749801e+100 1.749801e+100 13.931355 26.199966 2 1.749801e+100 9.929842e-01 5.936691 0.800000 3 1.749801e+100 9.929842e-01 5.936691 0.800000 4 4.800000e-01 1.749801e+100 6.872657 NaN 5 4.800000e-01 1.749801e+100 6.872657 NaN 6 3.451000e-01 1.550366e+00 5.474547 26.399994 7 3.451000e-01 1.550366e+00 5.474547 26.399994 8 4.700000e-01 9.523810e-01 1.260353 NaN

The test file used above is \sg99\dta\aidsdata.dta contained in https://web.archive.org/web/19991011204243/http://lib.stat.cmu.edu/stata/STB/stb47v5.zip

References:
[1] Stata 1 reference manual, page 5 (https://www.statalist.org/forums/forum/general-stata-discussion/general/1638352-stata-1-reference-manual-now-available-to-anyone-who-wants-it)
[2] Description of the .dta file format 108, Representation of numbers (dta_108.txt)

Expected Behavior

I would expect the data displayed to match that in Stata:

. dtaversion "aidsdata.dta"
  (file "aidsdata.dta" is .dta-format 105 from Stata 5)

. use "aidsdata.dta"
(X-cntry data on HIV & Dtrmnts)

. list gini urbmf miltpop gaplit in 1/10

     +-------------------------------------+
     |  gini   urbmf    miltpop     gaplit |
     |-------------------------------------|
  1. | 0.390   1.029   10.66892   26.99999 |
  2. |     .       .   13.93135   26.19997 |
  3. |     .   0.993   5.936691   .8000002 |
  4. |     .   0.993   5.936691   .8000002 |
  5. | 0.480       .   6.872657          . |
     |-------------------------------------|
  6. | 0.480       .   6.872657          . |
  7. | 0.345   1.550   5.474547   26.39999 |
  8. | 0.345   1.550   5.474547   26.39999 |
  9. | 0.470   0.952   1.260353          . |
 10. |     .   1.092   2.129809         14 |
     +-------------------------------------+

Installed Versions

INSTALLED VERSIONS

commit : bdc79c1
python : 3.12.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.2.0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : 2.9.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None