Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" support numpy compatibility across versions """ 

2 

3from distutils.version import LooseVersion 

4import re 

5 

6import numpy as np 

7 

8# numpy versioning 

9_np_version = np.__version__ 

10_nlv = LooseVersion(_np_version) 

11_np_version_under1p14 = _nlv < LooseVersion("1.14") 

12_np_version_under1p15 = _nlv < LooseVersion("1.15") 

13_np_version_under1p16 = _nlv < LooseVersion("1.16") 

14_np_version_under1p17 = _nlv < LooseVersion("1.17") 

15_np_version_under1p18 = _nlv < LooseVersion("1.18") 

16_np_version_under1p19 = _nlv < LooseVersion("1.19") 

17_np_version_under1p20 = _nlv < LooseVersion("1.20") 

18_is_numpy_dev = ".dev" in str(_nlv) 

19 

20 

21if _nlv < "1.13.3": 

22 raise ImportError( 

23 f"this version of pandas is incompatible with " 

24 f"numpy < 1.13.3\n" 

25 f"your numpy version is {_np_version}.\n" 

26 f"Please upgrade numpy to >= 1.13.3 to use " 

27 f"this pandas version" 

28 ) 

29 

30 

31_tz_regex = re.compile("[+-]0000$") 

32 

33 

34def tz_replacer(s): 

35 if isinstance(s, str): 

36 if s.endswith("Z"): 

37 s = s[:-1] 

38 elif _tz_regex.search(s): 

39 s = s[:-5] 

40 return s 

41 

42 

43def np_datetime64_compat(s, *args, **kwargs): 

44 """ 

45 provide compat for construction of strings to numpy datetime64's with 

46 tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation 

47 warning, when need to pass '2015-01-01 09:00:00' 

48 """ 

49 s = tz_replacer(s) 

50 return np.datetime64(s, *args, **kwargs) 

51 

52 

53def np_array_datetime64_compat(arr, *args, **kwargs): 

54 """ 

55 provide compat for construction of an array of strings to a 

56 np.array(..., dtype=np.datetime64(..)) 

57 tz-changes in 1.11 that make '2015-01-01 09:00:00Z' show a deprecation 

58 warning, when need to pass '2015-01-01 09:00:00' 

59 """ 

60 # is_list_like 

61 if hasattr(arr, "__iter__") and not isinstance(arr, (str, bytes)): 

62 arr = [tz_replacer(s) for s in arr] 

63 else: 

64 arr = tz_replacer(arr) 

65 

66 return np.array(arr, *args, **kwargs) 

67 

68 

69__all__ = [ 

70 "np", 

71 "_np_version", 

72 "_np_version_under1p14", 

73 "_np_version_under1p15", 

74 "_np_version_under1p16", 

75 "_np_version_under1p17", 

76 "_is_numpy_dev", 

77]