__init__.py 2.1 KB

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