__init__.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """ support numpy compatibility across versions """
  2. from distutils.version import LooseVersion
  3. import re
  4. import numpy as np
  5. # numpy versioning
  6. _np_version = np.__version__
  7. _nlv = LooseVersion(_np_version)
  8. _np_version_under1p14 = _nlv < LooseVersion("1.14")
  9. _np_version_under1p15 = _nlv < LooseVersion("1.15")
  10. _np_version_under1p16 = _nlv < LooseVersion("1.16")
  11. _np_version_under1p17 = _nlv < LooseVersion("1.17")
  12. _np_version_under1p18 = _nlv < LooseVersion("1.18")
  13. _is_numpy_dev = ".dev" in str(_nlv)
  14. if _nlv < "1.13.3":
  15. raise ImportError(
  16. f"this version of pandas is incompatible with "
  17. f"numpy < 1.13.3\n"
  18. f"your numpy version is {_np_version}.\n"
  19. f"Please upgrade numpy to >= 1.13.3 to use "
  20. f"this pandas version"
  21. )
  22. _tz_regex = re.compile("[+-]0000$")
  23. def tz_replacer(s):
  24. if isinstance(s, str):
  25. if s.endswith("Z"):
  26. s = s[:-1]
  27. elif _tz_regex.search(s):
  28. s = s[:-5]
  29. return s
  30. def np_datetime64_compat(s, *args, **kwargs):
  31. """
  32. provide compat for construction of strings to numpy datetime64's with
  33. tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
  34. warning, when need to pass '2015-01-01 09:00:00'
  35. """
  36. s = tz_replacer(s)
  37. return np.datetime64(s, *args, **kwargs)
  38. def np_array_datetime64_compat(arr, *args, **kwargs):
  39. """
  40. provide compat for construction of an array of strings to a
  41. np.array(..., dtype=np.datetime64(..))
  42. tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation
  43. warning, when need to pass '2015-01-01 09:00:00'
  44. """
  45. # is_list_like
  46. if hasattr(arr, "__iter__") and not isinstance(arr, (str, bytes)):
  47. arr = [tz_replacer(s) for s in arr]
  48. else:
  49. arr = tz_replacer(arr)
  50. return np.array(arr, *args, **kwargs)
  51. __all__ = [
  52. "np",
  53. "_np_version",
  54. "_np_version_under1p14",
  55. "_np_version_under1p15",
  56. "_np_version_under1p16",
  57. "_np_version_under1p17",
  58. "_is_numpy_dev",
  59. ]