diff --git a/czsc/__init__.py b/czsc/__init__.py index f392eb364..4395bec0a 100644 --- a/czsc/__init__.py +++ b/czsc/__init__.py @@ -47,6 +47,7 @@ from czsc.utils import ( mac_address, overlap, + to_arrow, format_standard_kline, @@ -65,6 +66,7 @@ save_json, get_sub_elements, get_py_namespace, + code_namespace, freqs_sorted, x_round, import_by_name, diff --git a/czsc/utils/__init__.py b/czsc/utils/__init__.py index c991f5b0c..5d96e7e39 100644 --- a/czsc/utils/__init__.py +++ b/czsc/utils/__init__.py @@ -95,6 +95,20 @@ def get_py_namespace(file_py: str, keys: list = []) -> dict: return namespace +def code_namespace(code: str, keys: list = []) -> dict: + """获取 python 代码中的 namespace + + :param code: python 代码 + :param keys: 指定需要的对象名称 + :return: namespace + """ + namespace = {"code": code} + exec(code, namespace) + if keys: + namespace = {k: v for k, v in namespace.items() if k in keys} + return namespace + + def import_by_name(name): """通过字符串导入模块、类、函数 @@ -199,3 +213,15 @@ def mac_address(): x = uuid.UUID(int=uuid.getnode()).hex[-12:].upper() x = "-".join([x[i : i + 2] for i in range(0, 11, 2)]) return x + + +def to_arrow(df: pd.DataFrame): + """将 pandas.DataFrame 转换为 pyarrow.Table""" + import io + import pyarrow as pa + + table = pa.Table.from_pandas(df) + with io.BytesIO() as sink: + with pa.ipc.new_file(sink, table.schema) as writer: + writer.write_table(table) + return sink.getvalue() diff --git a/czsc/utils/bar_generator.py b/czsc/utils/bar_generator.py index b5c63dda1..558361988 100644 --- a/czsc/utils/bar_generator.py +++ b/czsc/utils/bar_generator.py @@ -31,6 +31,7 @@ def is_trading_time(dt: datetime = datetime.now(), market="A股"): def get_intraday_times(freq="1分钟", market="A股"): """获取指定市场的交易时间段 + :param freq: K线周期,如 1分钟、5分钟、15分钟、30分钟、60分钟 :param market: 市场名称,可选值:A股、期货、默认 :return: 交易时间段列表 """