MAINT: Prepare patsy for pandas3 StringDtype - #229
Conversation
4001d9e to
6a1e812
Compare
2ceaeab to
f282fae
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR prepares the patsy library for pandas 3 compatibility by adding support for StringDtype and fixing tests that are incompatible with pandas 3's copy-on-write behavior. The changes ensure the library continues to work with newer pandas versions while maintaining backward compatibility.
- Adds detection logic for pandas 3 and StringDtype support
- Updates test assertions to account for copy-on-write behavior in pandas 3
- Extends dtype checking functions to handle StringDtype alongside existing categorical dtype support
c15476b to
f15e36c
Compare
| good_mask = ~total_mask | ||
| # "..." to handle 1- versus 2-dim indexing | ||
| return [v[good_mask, ...] for v in values] | ||
| return [v[good_mask] if v.ndim == 1 else v[good_mask, ...] for v in values] |
There was a problem hiding this comment.
Is this due to upstream indexing changes? Kind of annoying if ... no longer supports "zero" expansion.
There was a problem hiding this comment.
Yes, it appears that it is stricter and no longer supports expansion. I only found this by running against the statsmodels test suite.
There was a problem hiding this comment.
Hmmm... this still works in my testing. Is there an upstream numpy change I should be worried about? (These are, or should be, numpy arrays still).
| # https://github.com/pydata/pandas/issues/9581#issuecomment-77099564 | ||
| def safe_issubdtype(dt1, dt2): | ||
| if safe_is_pandas_categorical_dtype(dt1): | ||
| if safe_is_pandas_categorical_dtype(dt1) or safe_is_pandas_string_dtype(dt1): |
There was a problem hiding this comment.
Hmmm... are there other places that string dtypes should be treated as categorical, no? I'll need to take a look, since I haven't looked at patsy code for a while.
There was a problem hiding this comment.
Doesn't look like it, but I didn't do an exhaustive search.
matthewwardrop
left a comment
There was a problem hiding this comment.
Left a few questions/feedback. Once we've addressed those, let's get this in and released.
| good_mask = ~total_mask | ||
| # "..." to handle 1- versus 2-dim indexing | ||
| return [v[good_mask, ...] for v in values] | ||
| return [v[good_mask] if v.ndim == 1 else v[good_mask, ...] for v in values] |
There was a problem hiding this comment.
Hmmm... this still works in my testing. Is there an upstream numpy change I should be worried about? (These are, or should be, numpy arrays still).
| # https://github.com/pydata/pandas/issues/9581#issuecomment-77099564 | ||
| def safe_issubdtype(dt1, dt2): | ||
| if safe_is_pandas_categorical_dtype(dt1): | ||
| if safe_is_pandas_categorical_dtype(dt1) or safe_is_pandas_string_dtype(dt1): |
There was a problem hiding this comment.
Doesn't look like it, but I didn't do an exhaustive search.
Adds support for StringDtype Fixes tests that are not valid with copy-on-write
Special 1 and 2 d cases when indexing for pandas 3 support
cb2f01c to
6549241
Compare
cbdec7c to
f35bf45
Compare
This is probably a subtle bug in patsy. If you:
then patsy passes pandas series to the NA handler. pandas 3 changed the semantics about Series indexing, and so this is why this fix is included here. This is the statsmodels call that hits this Formula looks like data is and |
|
I wrote a test that will fail using existing pandas. It requries using a non-trivial index, e.g., strings. |
|
LGTM. Thanks @bashtage ! |
Adds support for StringDtype
Fixes tests that are not valid with copy-on-write