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

1from typing import Union 

2import numpy as np 

3import pandas as pd 

4import modin.pandas as mpd 

5from datetime import datetime, timedelta 

6import calendar 

7 

8 

9def convert_date(date: Union[datetime, str, pd.Series, np.ndarray]) -> np.ndarray: 

10 """Receives `date` from a variety of datatypes and converts it into a numeric value in a numpy array. 

11 

12 If `date` is a vector then it returns a vector otherwise it returns a single scalar value. 

13 

14 Args: 

15 date (Union[datetime, str, pd.Series, np.ndarray]): The date to be converted 

16 

17 Returns: 

18 np.ndarray: A NumPy array with datatype datetime64[D]. 

19 """ 

20 if isinstance(date, int): 

21 date = pd.to_datetime(str(date)) 

22 elif isinstance(date, float): 

23 year = int(date) 

24 days_in_year = 366 if calendar.isleap(year) else 365 

25 date = datetime(year, 1, 1) + timedelta(days=(date % 1) * days_in_year) 

26 elif isinstance(date, np.ndarray): 

27 if np.issubdtype(date.dtype, np.integer): 

28 date = date.astype(str) 

29 date = pd.to_datetime(date) 

30 elif type(date) == mpd.Series: 

31 date = mpd.to_datetime(date) 

32 else: 

33 date = pd.to_datetime(date) 

34 

35 return np.array(date, dtype="datetime64[D]") 

36 

37 

38def timestamp_to_decimal_year(date): 

39 return np.array(date.year + (date.dayofyear - 1) / (365.0 + date.is_leap_year * 1.0)) 

40 

41 

42def date_time_to_decimal_year( 

43 date: Union[ 

44 datetime, 

45 pd.Timestamp, 

46 list, 

47 tuple, 

48 np.datetime64, 

49 int, 

50 float, 

51 str, 

52 pd.Series, 

53 np.ndarray, 

54 ] 

55) -> np.ndarray: 

56 """ 

57 Converts a date from a variety of formats to be a decimal year. 

58 

59 Args: 

60 date (Union[ datetime, pd.Timestamp, list, tuple, np.datetime64, int, float, str, pd.Series, np.ndarray, ]): 

61 The date to be converted. 

62 

63 Returns: 

64 np.ndarray: The date as a NumPy array of the same shape as the input. 

65 """ 

66 # If the date is a list or a tuple, then convert to a numpy array before doing anything else 

67 if isinstance(date, (list, tuple)): 

68 date = np.array(date) 

69 

70 if isinstance(date, (float, int)): 

71 # if a scalar numerical value, then assume that this is already as a numerical date 

72 return np.array([date]) 

73 elif isinstance(date, (datetime, pd.Timestamp, np.datetime64)): 

74 # if a scalar date value, then convert to pandas to be converted to decimal year 

75 return timestamp_to_decimal_year(pd.to_datetime(date)) 

76 elif isinstance(date, (pd.Series, mpd.Series, np.ndarray)): 

77 if date.dtype in [float, int]: 

78 # if it is already an array of numerical values, then just return it 

79 return date 

80 

81 return timestamp_to_decimal_year(pd.to_datetime(convert_date(date)))