opsci-toolbox 0.0.5__py3-none-any.whl → 0.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opsci_toolbox/apis/rapidapi_helpers.py +1 -0
- opsci_toolbox/helpers/common.py +557 -207
- opsci_toolbox/helpers/cv.py +298 -123
- opsci_toolbox/helpers/dataviz.py +875 -191
- opsci_toolbox/helpers/dates.py +55 -8
- opsci_toolbox/helpers/nlp.py +746 -97
- opsci_toolbox/helpers/nlp_cuml.py +166 -57
- opsci_toolbox/helpers/sna.py +101 -10
- opsci_toolbox/helpers/surreaction.py +58 -16
- {opsci_toolbox-0.0.5.dist-info → opsci_toolbox-0.0.6.dist-info}/METADATA +2 -1
- opsci_toolbox-0.0.6.dist-info/RECORD +21 -0
- opsci_toolbox-0.0.5.dist-info/RECORD +0 -21
- {opsci_toolbox-0.0.5.dist-info → opsci_toolbox-0.0.6.dist-info}/WHEEL +0 -0
- {opsci_toolbox-0.0.5.dist-info → opsci_toolbox-0.0.6.dist-info}/top_level.txt +0 -0
opsci_toolbox/helpers/dates.py
CHANGED
@@ -1,13 +1,31 @@
|
|
1
1
|
from datetime import datetime, timezone
|
2
2
|
import pandas as pd
|
3
3
|
|
4
|
-
def get_now():
|
4
|
+
def get_now() -> datetime:
|
5
|
+
"""
|
6
|
+
Gets the current datetime in Coordinated Universal Time (UTC).
|
7
|
+
|
8
|
+
Returns:
|
9
|
+
- now (datetime): The current datetime in UTC.
|
10
|
+
"""
|
5
11
|
return datetime.now(timezone.utc)
|
6
12
|
|
7
|
-
def str_to_datetime(date_string, format = "%a %b %d %H:%M:%S %z %Y"):
|
8
|
-
|
9
|
-
|
10
|
-
|
13
|
+
def str_to_datetime(date_string: str, format: str = "%a %b %d %H:%M:%S %z %Y") -> str:
|
14
|
+
"""
|
15
|
+
Converts a string representation of a datetime to a datetime object.
|
16
|
+
|
17
|
+
Parameters:
|
18
|
+
- date_string (str): The string representation of the datetime.
|
19
|
+
- format (str, optional): The format of the datetime string. Defaults to "%a %b %d %H:%M:%S %z %Y".
|
20
|
+
-- Facebook Format : "2024-02-13T15:20:23+0000" = "%Y-%m-%dT%H:%M:%S%z"
|
21
|
+
-- Youtube : '1970-01-01T00:00:00Z' = "%Y-%m-%dT%H:%M:%SZ"
|
22
|
+
-- Twitter RapidAPI : '%a %b %d %H:%M:%S %z %Y'
|
23
|
+
|
24
|
+
Returns:
|
25
|
+
- formated_date (Union[datetime, str]): The datetime object if conversion is successful,
|
26
|
+
otherwise the original string.
|
27
|
+
"""
|
28
|
+
|
11
29
|
try:
|
12
30
|
formated_date = datetime.strptime(date_string, format)
|
13
31
|
return formated_date
|
@@ -16,18 +34,47 @@ def str_to_datetime(date_string, format = "%a %b %d %H:%M:%S %z %Y"):
|
|
16
34
|
print(e)
|
17
35
|
return date_string
|
18
36
|
|
37
|
+
def datetime_to_str(date: datetime, date_format: str = '%Y-%m-%dT%H:%M:%SZ') -> str:
|
38
|
+
"""
|
39
|
+
Converts a datetime object to a string representation.
|
40
|
+
|
41
|
+
Parameters:
|
42
|
+
- date (datetime): The datetime object to convert.
|
43
|
+
- date_format (str, optional): The format of the output datetime string. Defaults to '%Y-%m-%dT%H:%M:%SZ'.
|
19
44
|
|
20
|
-
|
45
|
+
Returns:
|
46
|
+
- str_date (str): The string representation of the datetime object.
|
47
|
+
"""
|
21
48
|
return date.strftime(date_format)
|
22
49
|
|
23
|
-
def number_of_days(start_date, end_date):
|
50
|
+
def number_of_days(start_date: datetime, end_date: datetime) -> int:
|
51
|
+
"""
|
52
|
+
Calculates the number of days between two datetime objects.
|
53
|
+
|
54
|
+
Parameters:
|
55
|
+
- start_date (datetime): The start date.
|
56
|
+
- end_date (datetime): The end date.
|
57
|
+
|
58
|
+
Returns:
|
59
|
+
- days_difference (int): The number of days between the start and end dates.
|
60
|
+
"""
|
24
61
|
# Calculate the difference
|
25
62
|
time_difference = start_date - end_date
|
26
63
|
# Extract the number of days from the timedelta object
|
27
64
|
days_difference = time_difference.days
|
28
65
|
return days_difference
|
29
66
|
|
30
|
-
def df_col_to_datetime(df, col):
|
67
|
+
def df_col_to_datetime(df: pd.DataFrame, col: str) -> pd.DataFrame:
|
68
|
+
"""
|
69
|
+
Converts a column in a pandas DataFrame to datetime format.
|
70
|
+
|
71
|
+
Parameters:
|
72
|
+
- df (pd.DataFrame): The pandas DataFrame.
|
73
|
+
- col (str): The name of the column to convert to datetime.
|
74
|
+
|
75
|
+
Returns:
|
76
|
+
- df (pd.DataFrame): The DataFrame with the specified column converted to datetime format.
|
77
|
+
"""
|
31
78
|
df[col] = pd.to_datetime(df[col])
|
32
79
|
return df
|
33
80
|
|