Skip to content

Commit 958bd89

Browse files
committed
works again when pandas isn't installed
1 parent 02d6e26 commit 958bd89

File tree

2 files changed

+70
-56
lines changed

2 files changed

+70
-56
lines changed

stringdtype/stringdtype/src/umath.c

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,6 @@ init_ufuncs(void)
391391
goto error;
392392
}
393393

394-
PyArray_DTypeMeta *peq_dtypes[] = {(PyArray_DTypeMeta *)&PandasStringDType,
395-
(PyArray_DTypeMeta *)&PandasStringDType,
396-
&PyArray_BoolDType};
397-
398-
if (init_ufunc(numpy, "equal", peq_dtypes,
399-
&string_equal_resolve_descriptors,
400-
&string_equal_strided_loop, "string_equal", 2, 1,
401-
NPY_NO_CASTING, 0) < 0) {
402-
goto error;
403-
}
404-
405394
PyArray_DTypeMeta *promoter_dtypes[2][3] = {
406395
{(PyArray_DTypeMeta *)&StringDType, &PyArray_UnicodeDType,
407396
&PyArray_BoolDType},
@@ -417,21 +406,6 @@ init_ufuncs(void)
417406
goto error;
418407
}
419408

420-
PyArray_DTypeMeta *p_promoter_dtypes[2][3] = {
421-
{(PyArray_DTypeMeta *)&PandasStringDType, &PyArray_UnicodeDType,
422-
&PyArray_BoolDType},
423-
{&PyArray_UnicodeDType, (PyArray_DTypeMeta *)&PandasStringDType,
424-
&PyArray_BoolDType},
425-
};
426-
427-
if (add_promoter(numpy, "equal", p_promoter_dtypes[0]) < 0) {
428-
goto error;
429-
}
430-
431-
if (add_promoter(numpy, "equal", p_promoter_dtypes[1]) < 0) {
432-
goto error;
433-
}
434-
435409
PyArray_DTypeMeta *isnan_dtypes[] = {(PyArray_DTypeMeta *)&StringDType,
436410
&PyArray_BoolDType};
437411

@@ -442,16 +416,6 @@ init_ufuncs(void)
442416
goto error;
443417
}
444418

445-
PyArray_DTypeMeta *p_isnan_dtypes[] = {
446-
(PyArray_DTypeMeta *)&PandasStringDType, &PyArray_BoolDType};
447-
448-
if (init_ufunc(numpy, "isnan", p_isnan_dtypes,
449-
&string_isnan_resolve_descriptors,
450-
&string_isnan_strided_loop, "string_isnan", 1, 1,
451-
NPY_NO_CASTING, 0) < 0) {
452-
goto error;
453-
}
454-
455419
PyArray_DTypeMeta *minmax_dtypes[] = {
456420
(PyArray_DTypeMeta *)&StringDType,
457421
(PyArray_DTypeMeta *)&StringDType,
@@ -469,6 +433,57 @@ init_ufuncs(void)
469433
goto error;
470434
}
471435

436+
PyArray_DTypeMeta *add_dtypes[] = {
437+
(PyArray_DTypeMeta *)&StringDType,
438+
(PyArray_DTypeMeta *)&StringDType,
439+
(PyArray_DTypeMeta *)&StringDType,
440+
};
441+
442+
if (init_ufunc(numpy, "add", add_dtypes, NULL, &add_strided_loop,
443+
"string_add", 2, 1, NPY_NO_CASTING, 0) < 0) {
444+
goto error;
445+
}
446+
447+
if (!PANDAS_AVAILABLE) {
448+
goto finish;
449+
}
450+
451+
PyArray_DTypeMeta *peq_dtypes[] = {(PyArray_DTypeMeta *)&PandasStringDType,
452+
(PyArray_DTypeMeta *)&PandasStringDType,
453+
&PyArray_BoolDType};
454+
455+
if (init_ufunc(numpy, "equal", peq_dtypes,
456+
&string_equal_resolve_descriptors,
457+
&string_equal_strided_loop, "string_equal", 2, 1,
458+
NPY_NO_CASTING, 0) < 0) {
459+
goto error;
460+
}
461+
462+
PyArray_DTypeMeta *p_promoter_dtypes[2][3] = {
463+
{(PyArray_DTypeMeta *)&PandasStringDType, &PyArray_UnicodeDType,
464+
&PyArray_BoolDType},
465+
{&PyArray_UnicodeDType, (PyArray_DTypeMeta *)&PandasStringDType,
466+
&PyArray_BoolDType},
467+
};
468+
469+
if (add_promoter(numpy, "equal", p_promoter_dtypes[0]) < 0) {
470+
goto error;
471+
}
472+
473+
if (add_promoter(numpy, "equal", p_promoter_dtypes[1]) < 0) {
474+
goto error;
475+
}
476+
477+
PyArray_DTypeMeta *p_isnan_dtypes[] = {
478+
(PyArray_DTypeMeta *)&PandasStringDType, &PyArray_BoolDType};
479+
480+
if (init_ufunc(numpy, "isnan", p_isnan_dtypes,
481+
&string_isnan_resolve_descriptors,
482+
&string_isnan_strided_loop, "string_isnan", 1, 1,
483+
NPY_NO_CASTING, 0) < 0) {
484+
goto error;
485+
}
486+
472487
PyArray_DTypeMeta *p_minmax_dtypes[] = {
473488
(PyArray_DTypeMeta *)&PandasStringDType,
474489
(PyArray_DTypeMeta *)&PandasStringDType,
@@ -487,17 +502,6 @@ init_ufuncs(void)
487502
goto error;
488503
}
489504

490-
PyArray_DTypeMeta *add_dtypes[] = {
491-
(PyArray_DTypeMeta *)&StringDType,
492-
(PyArray_DTypeMeta *)&StringDType,
493-
(PyArray_DTypeMeta *)&StringDType,
494-
};
495-
496-
if (init_ufunc(numpy, "add", add_dtypes, NULL, &add_strided_loop,
497-
"string_add", 2, 1, NPY_NO_CASTING, 0) < 0) {
498-
goto error;
499-
}
500-
501505
PyArray_DTypeMeta *p_add_dtypes[] = {
502506
(PyArray_DTypeMeta *)&PandasStringDType,
503507
(PyArray_DTypeMeta *)&PandasStringDType,
@@ -509,6 +513,7 @@ init_ufuncs(void)
509513
goto error;
510514
}
511515

516+
finish:
512517
Py_DECREF(numpy);
513518
return 0;
514519

stringdtype/tests/test_stringdtype.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import tempfile
66

77
import numpy as np
8-
import pandas
8+
9+
try:
10+
import pandas
11+
except ImportError:
12+
pandas = None
913
import pytest
1014

1115
from stringdtype import (
@@ -22,16 +26,17 @@ def string_list():
2226
return ["abc", "def", "ghi", "A¢☃€ 😊", "Abc", "DEF"]
2327

2428

25-
@pytest.fixture(params=["StringDtype", "PandasStringDType"])
29+
@pytest.fixture(params=["StringDType", "PandasStringDType"])
2630
def dtype(request):
2731
if request.param == "StringDType":
2832
return StringDType()
29-
try:
30-
from stringdtype import PandasStringDType
33+
elif request.param == "PandasStringDType":
34+
try:
35+
from stringdtype import PandasStringDType
3136

32-
return PandasStringDType()
33-
except ImportError:
34-
pytest.skip("cannot import pandas")
37+
return PandasStringDType()
38+
except ImportError:
39+
pytest.skip("cannot import pandas")
3540

3641

3742
@pytest.fixture
@@ -361,9 +366,13 @@ def test_ufunc_add(dtype, string_list, other_strings):
361366
)
362367

363368

364-
@pytest.mark.parametrize("na_val", [float("nan"), np.nan, NA, pandas.NA])
369+
@pytest.mark.parametrize(
370+
"na_val", [float("nan"), np.nan, NA, getattr(pandas, "NA", None)]
371+
)
365372
def test_create_with_na(dtype, na_val):
366-
if dtype == StringDType() and na_val is pandas.NA:
373+
if not hasattr(pandas, "NA") or (
374+
dtype == StringDType() and na_val is pandas.NA
375+
):
367376
return
368377
if dtype != StringDType and na_val is NA:
369378
return

0 commit comments

Comments
 (0)