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"""This module is designed for community supported date conversion functions""" 

2import numpy as np 

3 

4from pandas._libs.tslibs import parsing 

5 

6 

7def parse_date_time(date_col, time_col): 

8 date_col = _maybe_cast(date_col) 

9 time_col = _maybe_cast(time_col) 

10 return parsing.try_parse_date_and_time(date_col, time_col) 

11 

12 

13def parse_date_fields(year_col, month_col, day_col): 

14 year_col = _maybe_cast(year_col) 

15 month_col = _maybe_cast(month_col) 

16 day_col = _maybe_cast(day_col) 

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

18 

19 

20def parse_all_fields(year_col, month_col, day_col, hour_col, minute_col, second_col): 

21 year_col = _maybe_cast(year_col) 

22 month_col = _maybe_cast(month_col) 

23 day_col = _maybe_cast(day_col) 

24 hour_col = _maybe_cast(hour_col) 

25 minute_col = _maybe_cast(minute_col) 

26 second_col = _maybe_cast(second_col) 

27 return parsing.try_parse_datetime_components( 

28 year_col, month_col, day_col, hour_col, minute_col, second_col 

29 ) 

30 

31 

32def generic_parser(parse_func, *cols): 

33 N = _check_columns(cols) 

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

35 

36 for i in range(N): 

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

38 results[i] = parse_func(*args) 

39 

40 return results 

41 

42 

43def _maybe_cast(arr): 

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

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

46 return arr 

47 

48 

49def _check_columns(cols): 

50 if not len(cols): 

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

52 

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

54 

55 N = len(head) 

56 

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

58 if n != N: 

59 raise AssertionError( 

60 f"All columns must have the same length: {N}; " 

61 f"column {i} has length {n}" 

62 ) 

63 

64 return N