Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/io/date_converters.py: 20%

47 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1"""This module is designed for community supported date conversion functions""" 

2from __future__ import annotations 

3 

4import warnings 

5 

6import numpy as np 

7 

8from pandas._libs.tslibs import parsing 

9from pandas._typing import npt 

10from pandas.util._exceptions import find_stack_level 

11 

12 

13def parse_date_time(date_col, time_col) -> npt.NDArray[np.object_]: 

14 """ 

15 Parse columns with dates and times into a single datetime column. 

16 

17 .. deprecated:: 1.2 

18 """ 

19 warnings.warn( 

20 """ 

21 Use pd.to_datetime(date_col + " " + time_col) instead to get a Pandas Series. 

22 Use pd.to_datetime(date_col + " " + time_col).to_pydatetime() instead to get a Numpy array. 

23""", # noqa: E501 

24 FutureWarning, 

25 stacklevel=find_stack_level(), 

26 ) 

27 date_col = _maybe_cast(date_col) 

28 time_col = _maybe_cast(time_col) 

29 return parsing.try_parse_date_and_time(date_col, time_col) 

30 

31 

32def parse_date_fields(year_col, month_col, day_col) -> npt.NDArray[np.object_]: 

33 """ 

34 Parse columns with years, months and days into a single date column. 

35 

36 .. deprecated:: 1.2 

37 """ 

38 warnings.warn( 

39 """ 

40 Use pd.to_datetime({"year": year_col, "month": month_col, "day": day_col}) instead to get a Pandas Series. 

41 Use ser = pd.to_datetime({"year": year_col, "month": month_col, "day": day_col}) and 

42 np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array. 

43""", # noqa: E501 

44 FutureWarning, 

45 stacklevel=find_stack_level(), 

46 ) 

47 

48 year_col = _maybe_cast(year_col) 

49 month_col = _maybe_cast(month_col) 

50 day_col = _maybe_cast(day_col) 

51 return parsing.try_parse_year_month_day(year_col, month_col, day_col) 

52 

53 

54def parse_all_fields( 

55 year_col, month_col, day_col, hour_col, minute_col, second_col 

56) -> npt.NDArray[np.object_]: 

57 """ 

58 Parse columns with datetime information into a single datetime column. 

59 

60 .. deprecated:: 1.2 

61 """ 

62 

63 warnings.warn( 

64 """ 

65 Use pd.to_datetime({"year": year_col, "month": month_col, "day": day_col, 

66 "hour": hour_col, "minute": minute_col, second": second_col}) instead to get a Pandas Series. 

67 Use ser = pd.to_datetime({"year": year_col, "month": month_col, "day": day_col, 

68 "hour": hour_col, "minute": minute_col, second": second_col}) and 

69 np.array([s.to_pydatetime() for s in ser]) instead to get a Numpy array. 

70""", # noqa: E501 

71 FutureWarning, 

72 stacklevel=find_stack_level(), 

73 ) 

74 

75 year_col = _maybe_cast(year_col) 

76 month_col = _maybe_cast(month_col) 

77 day_col = _maybe_cast(day_col) 

78 hour_col = _maybe_cast(hour_col) 

79 minute_col = _maybe_cast(minute_col) 

80 second_col = _maybe_cast(second_col) 

81 return parsing.try_parse_datetime_components( 

82 year_col, month_col, day_col, hour_col, minute_col, second_col 

83 ) 

84 

85 

86def generic_parser(parse_func, *cols) -> np.ndarray: 

87 """ 

88 Use dateparser to parse columns with data information into a single datetime column. 

89 

90 .. deprecated:: 1.2 

91 """ 

92 

93 warnings.warn( 

94 """ 

95 Use pd.to_datetime instead. 

96""", 

97 FutureWarning, 

98 stacklevel=find_stack_level(), 

99 ) 

100 

101 N = _check_columns(cols) 

102 results = np.empty(N, dtype=object) 

103 

104 for i in range(N): 

105 args = [c[i] for c in cols] 

106 results[i] = parse_func(*args) 

107 

108 return results 

109 

110 

111def _maybe_cast(arr: np.ndarray) -> np.ndarray: 

112 if not arr.dtype.type == np.object_: 

113 arr = np.array(arr, dtype=object) 

114 return arr 

115 

116 

117def _check_columns(cols) -> int: 

118 if not len(cols): 

119 raise AssertionError("There must be at least 1 column") 

120 

121 head, tail = cols[0], cols[1:] 

122 

123 N = len(head) 

124 

125 for i, n in enumerate(map(len, tail)): 

126 if n != N: 

127 raise AssertionError( 

128 f"All columns must have the same length: {N}; column {i} has length {n}" 

129 ) 

130 

131 return N