jzhou-utils 0.0.3__tar.gz
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.
- jzhou_utils-0.0.3/.gitignore +5 -0
- jzhou_utils-0.0.3/LICENSE +21 -0
- jzhou_utils-0.0.3/PKG-INFO +21 -0
- jzhou_utils-0.0.3/README.md +1 -0
- jzhou_utils-0.0.3/pyproject.toml +32 -0
- jzhou_utils-0.0.3/readme.md +1 -0
- jzhou_utils-0.0.3/src/jzhou_utils/__init__.py +3 -0
- jzhou_utils-0.0.3/src/jzhou_utils/base_utils.py +27 -0
- jzhou_utils-0.0.3/src/jzhou_utils/dt_utils.py +30 -0
- jzhou_utils-0.0.3/src/jzhou_utils/pd_utils.py +29 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 homage-to-the-square
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jzhou_utils
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: A small example package
|
|
5
|
+
Project-URL: Homepage, https://github.com/pypa/sampleproject
|
|
6
|
+
Project-URL: Issues, https://github.com/pypa/sampleproject/issues
|
|
7
|
+
Author-email: Jason Zhou <author@example.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Requires-Dist: datetime
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: pandas
|
|
16
|
+
Requires-Dist: re
|
|
17
|
+
Requires-Dist: requests
|
|
18
|
+
Requires-Dist: typing
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
This is a simple Python package for common data manipulations and other utils.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This is a simple Python package for common data manipulations and other utils.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "jzhou_utils"
|
|
8
|
+
version = "0.0.3"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"requests",
|
|
11
|
+
"numpy",
|
|
12
|
+
"pandas",
|
|
13
|
+
"datetime",
|
|
14
|
+
"typing",
|
|
15
|
+
"re"
|
|
16
|
+
]
|
|
17
|
+
authors = [
|
|
18
|
+
{ name="Jason Zhou", email="author@example.com" },
|
|
19
|
+
]
|
|
20
|
+
description = "A small example package"
|
|
21
|
+
readme = "README.md"
|
|
22
|
+
requires-python = ">=3.9"
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Programming Language :: Python :: 3",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
]
|
|
27
|
+
license = "MIT"
|
|
28
|
+
license-files = ["LICEN[CS]E*"]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/pypa/sampleproject"
|
|
32
|
+
Issues = "https://github.com/pypa/sampleproject/issues"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This is a simple Python package for common data manipulations and other utils.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import List, Set, Any
|
|
3
|
+
|
|
4
|
+
def map_dicts_values_to_keys(dict1, dict2) -> dict:
|
|
5
|
+
"""
|
|
6
|
+
creates a new dictionary, which maps from keys of dict1 to values of dict2,
|
|
7
|
+
assuming that the values of dict1 are keys of dict2
|
|
8
|
+
"""
|
|
9
|
+
return {k: dict2[v] for k, v in dict1.items() if v in dict2}
|
|
10
|
+
|
|
11
|
+
def is_decimal(string):
|
|
12
|
+
return bool(re.match(r"^-?\d+(\.\d+)?$", string))
|
|
13
|
+
|
|
14
|
+
def intersect_sets(sets: List[Set[Any]]) -> Set[Any]:
|
|
15
|
+
"""
|
|
16
|
+
Compute the intersection of a list of sets.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
sets (List[Set[Any]]): A list of set objects.
|
|
20
|
+
|
|
21
|
+
Returns:
|
|
22
|
+
Set[Any]: The intersection of all sets in the list.
|
|
23
|
+
"""
|
|
24
|
+
if not sets:
|
|
25
|
+
return set()
|
|
26
|
+
|
|
27
|
+
return set.intersection(*sets)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import datetime as dt
|
|
3
|
+
from typing import Union
|
|
4
|
+
|
|
5
|
+
def YYYMM_to_date(s: pd.Series) -> pd.Series:
|
|
6
|
+
"""
|
|
7
|
+
converts series of integers into dates
|
|
8
|
+
"""
|
|
9
|
+
return pd.to_datetime(s.astype(str), format='%Y%m').dt.date
|
|
10
|
+
|
|
11
|
+
def date_id_to_date(date_id: Union[int, pd.Series]):
|
|
12
|
+
if isinstance(date_id, int):
|
|
13
|
+
return dt.datetime.strptime(str(date_id), '%Y%m%d').date()
|
|
14
|
+
else:
|
|
15
|
+
return pd.to_datetime(date_id.astype(str), format='%Y%m%d').dt.date
|
|
16
|
+
|
|
17
|
+
def date_to_date_id(date: dt.date) -> int:
|
|
18
|
+
return int(date.strftime('%Y%m%d'))
|
|
19
|
+
|
|
20
|
+
def get_friday_of_isocalendar(iso_year, iso_week):
|
|
21
|
+
# Get the first day of the ISO week
|
|
22
|
+
first_day_of_week = dt.date(iso_year, 1, 1) + dt.timedelta(weeks=iso_week-1)
|
|
23
|
+
|
|
24
|
+
# Adjust to the correct day of the week (Friday is 4)
|
|
25
|
+
# `first_day_of_week.weekday()` gives the weekday of the first day of the ISO week.
|
|
26
|
+
# We need to adjust to make sure we get the Friday of that week.
|
|
27
|
+
days_to_friday = (4 - first_day_of_week.weekday()) % 7
|
|
28
|
+
friday = first_day_of_week + dt.timedelta(days=days_to_friday)
|
|
29
|
+
return friday
|
|
30
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Df utils:
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
def strip_excess_spaces_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
8
|
+
"""
|
|
9
|
+
Returns dataframe with leading + trailing spaces stripped for string columns
|
|
10
|
+
- from gpt, but note that this has high mem usage + slow effectiveness
|
|
11
|
+
"""
|
|
12
|
+
df_cleaned = df.copy()
|
|
13
|
+
for col in df.columns:
|
|
14
|
+
if df[col].apply(lambda x: isinstance(x, str)).mean() > 0.1:
|
|
15
|
+
df_cleaned[col] = df[col].str.rstrip()
|
|
16
|
+
return df_cleaned
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def str_to_float_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
20
|
+
"""
|
|
21
|
+
Try to convert all string columns into floats or ints, depending on pd.to_numeric
|
|
22
|
+
"""
|
|
23
|
+
df_converted = df.copy()
|
|
24
|
+
for col in df_converted.columns:
|
|
25
|
+
try:
|
|
26
|
+
df_converted[col] = pd.to_numeric(df_converted[col], errors='raise')
|
|
27
|
+
except Exception as e:
|
|
28
|
+
pass # Skip columns that can't be converted
|
|
29
|
+
return df_converted
|