Skip to content

Pandas 0.15 categorical handling - #59

Merged
njsmith merged 9 commits into
masterfrom
pandas-categorical-0.15
Apr 15, 2015
Merged

njsmith merged 9 commits into
masterfrom
pandas-categorical-0.15

Conversation

@njsmith

@njsmith njsmith commented Mar 4, 2015

Copy link
Copy Markdown
Member

Okay, here's my attempt to work around the changes in categorical handling in pandas 0.15.

@JanSchulz: I would very much appreciate a review, since this is heavily based on your patch in #47 (plus some refactoring to try and fix a few more issues). The individual commits are logical chunks, so it might be easier to read that way then as one giant patch.

I'd also really appreciate if you could write down a self-contained test that triggers the issue in pandas-dev/pandas#9581, or at least enough of an example to let me trigger it? I'm pretty sure I fixed it, but I'm not sure how to write a test case for it...

@kousu: I don't fully understand the issues you described in #47 (comment) , so I'm not sure whether I've fixed them or not :-). (They did make me squint harder at the interaction between C() and pandas categoricals, which helped me find a bug! But I don't think it was your bug.) If you happen to have the chance, I'd be interested in (a) whether installing this branch fixes your problems, (b) whether you could write a small self-contained test (i.e., avoiding read_csv and just calling patsy.dmatrix or patsy.dmatrices instead of using statsmodels) that triggers the issues you were seeing, so either so I can debug further (if it's not fixed), or add a test case (if it is fixed, so it will stay fixed).

@njsmith
njsmith force-pushed the pandas-categorical-0.15 branch from ed47ef6 to d40b54e Compare March 4, 2015 07:44
Comment thread patsy/util.py

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the problem could be here.

@jankatins

Copy link
Copy Markdown

@njsmith I think you can take the test from the original issue (untested):

data = """survived", "died", "treated", "risk"
12, 3,                "control", "low"
10, 2,                "treated", "low"
 6, 7,                "control", "medium"
10, 3,                "treated", "medium"
 3, 6,                "control", "high"
 8, 2,                "treated", "high"
"""
import pandas
import scipy.linalg; 
import scipy.lib.lapack.calc_lwork; 
scipy.linalg.calc_lwork = scipy.lib.lapack.calc_lwork #version creep bug: https://github.com/statsmodels/statsmodels/issues/2191
import statsmodels.api as sm #<-- this is the mark of a badly designed API: when you have a special subpackage just for your API

D = pd.read_csv(data)
def factor(series):
  "coerce series to a categorical variable in one line"
  "meant to be like R's factor() command"
  series = series.astype("category")
  series.cat.set_categories(set(series), inplace=True)
  # ^ from http://pandas-docs.github.io/pandas-docs-travis/categorical.html#getting-data-in-out
  return series

for C in ["treated", "risk"]:
  D[C] = factor(D[C])
M = sm.GLM.from_formula("died ~ treated", D)

@josef-pkt

Copy link
Copy Markdown

nosy comment to get myself on the notification list

@hmgaudecker

Copy link
Copy Markdown

Just a datapoint: This works with my code.

It would be wonderful if the fix found its way into patsy soonish, it is difficult to get co-authors compile install things by hand rather than a simple "conda install"...

@njsmith

njsmith commented Apr 15, 2015

Copy link
Copy Markdown
Member Author

@JanSchulz: I tried running the factor function and data you pasted together through old-patsy's dmatrices and everything worked fine. I don't know if this is because I someone broke the test while massaging it into something runnable, or because it got broken when initially extracted from the initial comment, or what. So I'd still like to see some sort of concrete reproducer if anyone has one... (CC @Kousa)

@njsmith

njsmith commented Apr 15, 2015

Copy link
Copy Markdown
Member Author

For some reason Travis is being super-slow, but the only change in my last commit was to a comment, and the version before that passed tests, so I guess I'll go ahead and merge this, and then if it breaks things we can clean that up after...

njsmith added a commit that referenced this pull request Apr 15, 2015
@njsmith
njsmith merged commit 3dd86d0 into master Apr 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants