index_class_helper.pxi.in 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. Template for functions of IndexEngine subclasses.
  3. WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
  4. """
  5. # ----------------------------------------------------------------------
  6. # IndexEngine Subclass Methods
  7. # ----------------------------------------------------------------------
  8. {{py:
  9. # name, dtype
  10. dtypes = [('Float64', 'float64'),
  11. ('Float32', 'float32'),
  12. ('Int64', 'int64'),
  13. ('Int32', 'int32'),
  14. ('Int16', 'int16'),
  15. ('Int8', 'int8'),
  16. ('UInt64', 'uint64'),
  17. ('UInt32', 'uint32'),
  18. ('UInt16', 'uint16'),
  19. ('UInt8', 'uint8'),
  20. ('Complex64', 'complex64'),
  21. ('Complex128', 'complex128'),
  22. ]
  23. engines = [('', 'IndexEngine'), ('Masked', 'MaskedIndexEngine')]
  24. }}
  25. {{for name, dtype in dtypes}}
  26. {{for prefix, engine in engines}}
  27. cdef class {{prefix}}{{name}}Engine({{engine}}):
  28. cdef _make_hash_table(self, Py_ssize_t n):
  29. {{if engine == 'MaskedIndexEngine'}}
  30. return _hash.{{name}}HashTable(n, uses_mask=True)
  31. {{else}}
  32. return _hash.{{name}}HashTable(n)
  33. {{endif}}
  34. cdef _check_type(self, object val):
  35. {{if engine == 'MaskedIndexEngine'}}
  36. if val is C_NA:
  37. return val
  38. {{endif}}
  39. {{if name not in {'Float64', 'Float32', 'Complex64', 'Complex128'} }}
  40. if not util.is_integer_object(val):
  41. if util.is_float_object(val):
  42. # Make sure Int64Index.get_loc(2.0) works
  43. if val.is_integer():
  44. return int(val)
  45. raise KeyError(val)
  46. {{if name.startswith("U")}}
  47. if val < 0:
  48. # cannot have negative values with unsigned int dtype
  49. raise KeyError(val)
  50. {{endif}}
  51. {{elif name not in {'Complex64', 'Complex128'} }}
  52. if not util.is_integer_object(val) and not util.is_float_object(val):
  53. # in particular catch bool and avoid casting True -> 1.0
  54. raise KeyError(val)
  55. {{else}}
  56. if (not util.is_integer_object(val)
  57. and not util.is_float_object(val)
  58. and not util.is_complex_object(val)
  59. ):
  60. # in particular catch bool and avoid casting True -> 1.0
  61. raise KeyError(val)
  62. {{endif}}
  63. return val
  64. {{endfor}}
  65. {{endfor}}