Skip to content

IO: StataWriter and np.inf #5761

Closed

Description

@jbrockmendel

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?

Activity

  1. bashtage commented on May 20, 2019

    @bashtage
    Member

    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.

  2. jbrockmendel commented on May 20, 2019

    @jbrockmendel
    ContributorAuthor

    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.

  3. jbrockmendel commented on May 21, 2019

    @jbrockmendel
    ContributorAuthor

    @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.inf entry from test_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.

  4. bashtage commented on May 21, 2019

    @bashtage
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions