Repository navigation
Closed
Description
TL;DR: is there a chance that pandas' StataWriter is doing a check that the statsmodels version should also be doing?
Looking at iolibs.foreign.StataReader and StataWriter and the updates pandas has made (their docstring specifically says it was based on Skipper's version here), I found that one of the statsmodels' tests raises when using the pandas version:
from statsmodels.iolib.tests.test_foreign import *
import pandas as pd
def test_missing_roundtrip():
buf = BytesIO()
dta = np.array([(np.nan, np.inf, "")],
dtype=[("double_miss", float), ("float_miss", np.float32),
("string_miss", "a1")])
writer = StataWriter(buf, dta)
writer.write_file()
buf.seek(0)
dta = genfromdta(buf, missing_flt=np.nan)
assert_(isnull(dta[0][0]))
assert_(isnull(dta[0][1]))
assert_(dta[0][2] == asbytes(""))
dta = genfromdta(os.path.join(curdir, "results/data_missing.dta"),
missing_flt=-999)
assert_(np.all([dta[0][i] == -999 for i in range(5)]))
def test_missing_roundtrip_pd():
buf = BytesIO()
dta = np.array([(np.nan, np.inf, "")],
dtype=[("double_miss", float), ("float_miss", np.float32),
("string_miss", "a1")])
df = pd.DataFrame(dta)
writer = StataWriter(buf, df) # <-- raises
The relevant part of the traceback here reads:
File "/usr/local/lib/python2.7/site-packages/pandas/io/stata.py", line 579, in _cast_to_stata_types
'supported by Stata.'.format(col=col))
ValueError: Column float_miss has a maximum value of infinity which is outside the range supported by Stata.
Does statsmodels need to implement the same check? If not, why not?
Metadata
Metadata
Assignees
Labels
No labels
Activity
bashtage commentedon May 20, 2019
IMO the statsmodels Stata code is so out-of-date and full of edge-case bugs that it should be completely abandoned in favor of the pandas version, which @kshedden and I have both improved and mostly kept up-to-date with Stata releases.
jbrockmendel commentedon May 20, 2019
I very much agree. A while back I made a PR to do that but the subtly different behavior meant the existing tests failed. So I'm currently working on a branch that wraps the pandas implementation and keeps the existing tests passing. This np.inf issue is a blocker for the StataWriter part of it.
jbrockmendel commentedon May 21, 2019
@bashtage it looks like you were the one who introduced this check in the pandas version in pandas-dev/pandas#14637. We can implement the same check here (indirectly by wrapping the pandas implementation) but will need to drop the
np.infentry fromtest_missing_roundtrip.If we're going to deprecate and eventually remove parts of iolib.foreign, we should try to get the deprecation in before the release. Given the length of the release cycle and all.
bashtage commentedon May 21, 2019
There is no inf in Stata, and pandas supports missing value in many ways, there is no reason to incorrectly encode a missing value as inf.
Why not try and add a FutureWarning on these files and encourage users(if any) to switch to pandas.