@@ -75,6 +75,8 @@ static PyType_Slot s2s_slots[] = {
7575 {0 , NULL }};
7676
7777static char * s2s_name = "cast_StringDType_to_StringDType" ;
78+ static char * s2p_name = "cast_StringDType_to_PandasStringDType" ;
79+ static char * p2s_name = "cast_PandasStringDType_to_StringDType" ;
7880
7981// unicode to string
8082
@@ -476,38 +478,83 @@ get_dtypes(PyArray_DTypeMeta *dt1, PyArray_DTypeMeta *dt2)
476478}
477479
478480PyArrayMethod_Spec * *
479- get_casts (void )
481+ get_casts (PyArray_DTypeMeta * this , PyArray_DTypeMeta * other ,
482+ int pandas_available )
480483{
481- PyArray_DTypeMeta * * s2s_dtypes = get_dtypes (NULL , NULL );
484+ PyArray_DTypeMeta * * s2s_dtypes = get_dtypes (this , this );
482485
483486 PyArrayMethod_Spec * StringToStringCastSpec =
484487 get_cast_spec (s2s_name , NPY_NO_CASTING ,
485488 NPY_METH_SUPPORTS_UNALIGNED , s2s_dtypes , s2s_slots );
486489
487- PyArray_DTypeMeta * * u2s_dtypes = get_dtypes (& PyArray_UnicodeDType , NULL );
490+ PyArrayMethod_Spec * ThisToOtherCastSpec = NULL ;
491+ PyArrayMethod_Spec * OtherToThisCastSpec = NULL ;
492+
493+ if (pandas_available ) {
494+ char * t2o_name = NULL ;
495+ char * o2t_name = NULL ;
496+
497+ if (this == (PyArray_DTypeMeta * )& StringDType ) {
498+ t2o_name = s2p_name ;
499+ o2t_name = p2s_name ;
500+ }
501+ else {
502+ t2o_name = p2s_name ;
503+ o2t_name = s2p_name ;
504+ }
505+
506+ PyArray_DTypeMeta * * t2o_dtypes = get_dtypes (this , other );
507+
508+ ThisToOtherCastSpec = get_cast_spec (t2o_name , NPY_NO_CASTING ,
509+ NPY_METH_SUPPORTS_UNALIGNED ,
510+ t2o_dtypes , s2s_slots );
511+
512+ PyArray_DTypeMeta * * o2t_dtypes = get_dtypes (other , this );
513+
514+ OtherToThisCastSpec = get_cast_spec (o2t_name , NPY_NO_CASTING ,
515+ NPY_METH_SUPPORTS_UNALIGNED ,
516+ o2t_dtypes , s2s_slots );
517+ }
518+
519+ PyArray_DTypeMeta * * u2s_dtypes = get_dtypes (& PyArray_UnicodeDType , this );
488520
489521 PyArrayMethod_Spec * UnicodeToStringCastSpec = get_cast_spec (
490522 u2s_name , NPY_SAFE_CASTING , NPY_METH_NO_FLOATINGPOINT_ERRORS ,
491523 u2s_dtypes , u2s_slots );
492524
493- PyArray_DTypeMeta * * s2u_dtypes = get_dtypes (NULL , & PyArray_UnicodeDType );
525+ PyArray_DTypeMeta * * s2u_dtypes = get_dtypes (this , & PyArray_UnicodeDType );
494526
495527 PyArrayMethod_Spec * StringToUnicodeCastSpec = get_cast_spec (
496528 s2u_name , NPY_SAFE_CASTING , NPY_METH_NO_FLOATINGPOINT_ERRORS ,
497529 s2u_dtypes , s2u_slots );
498530
499- PyArray_DTypeMeta * * s2b_dtypes = get_dtypes (NULL , & PyArray_BoolDType );
531+ PyArray_DTypeMeta * * s2b_dtypes = get_dtypes (this , & PyArray_BoolDType );
500532
501533 PyArrayMethod_Spec * StringToBoolCastSpec = get_cast_spec (
502534 s2b_name , NPY_UNSAFE_CASTING , NPY_METH_NO_FLOATINGPOINT_ERRORS ,
503535 s2b_dtypes , s2b_slots );
504536
505- PyArrayMethod_Spec * * casts = malloc (5 * sizeof (PyArrayMethod_Spec * ));
537+ PyArrayMethod_Spec * * casts = NULL ;
538+
539+ if (pandas_available ) {
540+ casts = malloc (7 * sizeof (PyArrayMethod_Spec * ));
541+ }
542+ else {
543+ casts = malloc (5 * sizeof (PyArrayMethod_Spec * ));
544+ }
545+
506546 casts [0 ] = StringToStringCastSpec ;
507547 casts [1 ] = UnicodeToStringCastSpec ;
508548 casts [2 ] = StringToUnicodeCastSpec ;
509549 casts [3 ] = StringToBoolCastSpec ;
510- casts [4 ] = NULL ;
550+ if (pandas_available ) {
551+ casts [4 ] = ThisToOtherCastSpec ;
552+ casts [5 ] = OtherToThisCastSpec ;
553+ casts [6 ] = NULL ;
554+ }
555+ else {
556+ casts [4 ] = NULL ;
557+ }
511558
512559 return casts ;
513560}
0 commit comments