{ "cells": [ { "cell_type": "markdown", "id": "999ce641-4370-4b92-bcfb-ae0f54bd387e", "metadata": {}, "source": [ "# 1. 对象的使用" ] }, { "cell_type": "code", "execution_count": 1, "id": "e4310406-a5bb-433b-8226-5b77407bd196", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on package tushare:\n", "\n", "NAME\n", " tushare - # -*- coding:utf-8 -*-\n", "\n", "PACKAGE CONTENTS\n", " bond (package)\n", " coins (package)\n", " data (package)\n", " fund (package)\n", " futures (package)\n", " internet (package)\n", " pro (package)\n", " stock (package)\n", " subs (package)\n", " trader (package)\n", " util (package)\n", "\n", "VERSION\n", " 1.2.89\n", "\n", "AUTHOR\n", " Jimmy Liu\n", "\n", "FILE\n", " c:\\program files\\python39\\lib\\site-packages\\tushare\\__init__.py\n", "\n", "\n" ] } ], "source": [ "import tushare\n", "\n", "help(tushare)" ] }, { "cell_type": "code", "execution_count": 7, "id": "104c8cee-46f3-4c11-a09b-81d64931a006", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "pro = tushare.pro_api(\"c850a58dc791dac495833a09300429a9ca87c15e05e71e5a60211fe0\")\n", "df = pro.daily(ts_code='000625.SZ', start_date='20180701', end_date='20231103')\n", "print(type(df))" ] }, { "cell_type": "code", "execution_count": 3, "id": "efd17a21-d876-4c16-b018-de84c3dda13e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on DataApi in module tushare.pro.client:\n", "\n", "functools.partial(>, '__origin__')\n" ] } ], "source": [ "help(pro)" ] }, { "cell_type": "code", "execution_count": 6, "id": "1f999fc8-ee15-4683-9c04-89c429418397", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 15.02\n", "1 15.00\n", "2 15.18\n", "3 15.51\n", "4 15.79\n", " ... \n", "1293 8.53\n", "1294 9.05\n", "1295 9.01\n", "1296 8.92\n", "1297 9.01\n", "Name: open, Length: 1298, dtype: float64\n" ] } ], "source": [ "data_open = df.open\n", "# print(data_open)" ] }, { "cell_type": "code", "execution_count": 9, "id": "582ad89b-7cbe-4b92-a283-da11611a3462", "metadata": {}, "outputs": [], "source": [ "import pandas.core.frame\n", "# help(pandas.core.frame.DataFrame)" ] }, { "cell_type": "code", "execution_count": 10, "id": "7f971058-5765-4a17-a3ae-5c3210828813", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import tushare as ts\n", "pro = ts.pro_api(\"c850a58dc791dac495833a09300429a9ca87c15e05e71e5a60211fe0\")\n", "df = pro.daily(ts_code='000625.SZ', start_date='20180701', end_date='20231103')\n", "print(type(df))" ] }, { "cell_type": "code", "execution_count": 13, "id": "74fd735f-e9c9-48d5-8c56-5ccc36396560", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RangeIndex(start=0, stop=1298, step=1)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.index" ] }, { "cell_type": "code", "execution_count": 12, "id": "d7c7e8f6-ecb4-46d2-9426-4ee0b22b6ffd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['ts_code', 'trade_date', 'open', 'high', 'low', 'close', 'pre_close',\n", " 'change', 'pct_chg', 'vol', 'amount'],\n", " dtype='object')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns" ] }, { "cell_type": "code", "execution_count": 14, "id": "bdbd43fb-31ac-46fd-b489-557e47641414", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1298, 11)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 18, "id": "0403524b-0d3e-4c3e-b184-8b5a7ef9e21d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([['000625.SZ', '20231103', 15.02, ..., 1.3926, 1436178.35,\n", " 2178791.073],\n", " ['000625.SZ', '20231102', 15.0, ..., 0.266, 1849642.08,\n", " 2806010.33],\n", " ['000625.SZ', '20231101', 15.18, ..., -0.5948, 1428837.43,\n", " 2158438.838],\n", " ...,\n", " ['000625.SZ', '20180704', 9.01, ..., 0.22, 107497.52, 96925.355],\n", " ['000625.SZ', '20180703', 8.92, ..., 1.01, 85118.73, 76084.956],\n", " ['000625.SZ', '20180702', 9.01, ..., -0.89, 141131.01, 126972.936]],\n", " dtype=object)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.values #[0,1] # ndarray" ] }, { "cell_type": "code", "execution_count": 19, "id": "e65a5b40-470a-4e83-8382-37d5257e7631", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15.0\n" ] } ], "source": [ "v = df.at[1, \"open\"]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 20, "id": "9ec2509f-8be1-4029-bd47-2d22e18be587", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15.0\n" ] } ], "source": [ "v = df.iat[1, 2]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 21, "id": "44deda84-4f73-440b-91fe-042a81ea9331", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ts_codetrade_dateopenhighlowclosepre_closechangepct_chgvolamount
0000625.SZ2023110315.0215.4014.8715.2915.080.211.39261436178.352178791.073
1000625.SZ2023110215.0015.4514.8515.0815.040.040.26601849642.082806010.330
2000625.SZ2023110115.1815.3114.8115.0415.13-0.09-0.59481428837.432158438.838
3000625.SZ2023103115.5115.8414.9715.1315.43-0.30-1.94432368677.713628313.928
4000625.SZ2023103015.7915.7915.1615.4315.81-0.38-2.40352495361.823857247.322
....................................
1293000625.SZ201807068.538.548.078.288.49-0.21-2.4700175904.46146602.257
1294000625.SZ201807059.059.138.918.949.03-0.09-1.0000129476.77116576.508
1295000625.SZ201807049.019.078.969.039.010.020.2200107497.5296925.355
1296000625.SZ201807038.929.018.839.018.920.091.010085118.7376084.956
1297000625.SZ201807029.019.118.808.929.00-0.08-0.8900141131.01126972.936
\n", "

1298 rows × 11 columns

\n", "
" ], "text/plain": [ " ts_code trade_date open high low close pre_close change \\\n", "0 000625.SZ 20231103 15.02 15.40 14.87 15.29 15.08 0.21 \n", "1 000625.SZ 20231102 15.00 15.45 14.85 15.08 15.04 0.04 \n", "2 000625.SZ 20231101 15.18 15.31 14.81 15.04 15.13 -0.09 \n", "3 000625.SZ 20231031 15.51 15.84 14.97 15.13 15.43 -0.30 \n", "4 000625.SZ 20231030 15.79 15.79 15.16 15.43 15.81 -0.38 \n", "... ... ... ... ... ... ... ... ... \n", "1293 000625.SZ 20180706 8.53 8.54 8.07 8.28 8.49 -0.21 \n", "1294 000625.SZ 20180705 9.05 9.13 8.91 8.94 9.03 -0.09 \n", "1295 000625.SZ 20180704 9.01 9.07 8.96 9.03 9.01 0.02 \n", "1296 000625.SZ 20180703 8.92 9.01 8.83 9.01 8.92 0.09 \n", "1297 000625.SZ 20180702 9.01 9.11 8.80 8.92 9.00 -0.08 \n", "\n", " pct_chg vol amount \n", "0 1.3926 1436178.35 2178791.073 \n", "1 0.2660 1849642.08 2806010.330 \n", "2 -0.5948 1428837.43 2158438.838 \n", "3 -1.9443 2368677.71 3628313.928 \n", "4 -2.4035 2495361.82 3857247.322 \n", "... ... ... ... \n", "1293 -2.4700 175904.46 146602.257 \n", "1294 -1.0000 129476.77 116576.508 \n", "1295 0.2200 107497.52 96925.355 \n", "1296 1.0100 85118.73 76084.956 \n", "1297 -0.8900 141131.01 126972.936 \n", "\n", "[1298 rows x 11 columns]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 23, "id": "29136991-3b67-4b1e-970a-1958e58db011", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " open close\n", "1 15.00 15.08\n", "2 15.18 15.04\n", "3 15.51 15.13\n", "4 15.79 15.43\n" ] } ], "source": [ "v = df.loc[[1,2,3,4],[\"open\", \"close\"]]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 24, "id": "27bdf386-4997-437e-9353-1108c9f1c599", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " open close\n", "1 15.00 15.08\n", "2 15.18 15.04\n", "3 15.51 15.13\n", "4 15.79 15.43\n" ] } ], "source": [ "v = df.iloc[[1,2,3,4],[2, 5]]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 26, "id": "7b048d8b-89d2-42b3-b8c3-2313dfd2d3c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " open close\n", "1 15.00 15.08\n", "2 15.18 15.04\n", "3 15.51 15.13\n", "4 15.79 15.43\n" ] } ], "source": [ "v = df.iloc[slice(1,5,1),[2, 5]]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 27, "id": "bc77eb67-f23e-4bfe-83ca-9ba6bfdc0c3e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " open close\n", "1 15.00 15.08\n", "2 15.18 15.04\n", "3 15.51 15.13\n", "4 15.79 15.43\n" ] } ], "source": [ "v = df.iloc[range(1,5,1),[2, 5]]\n", "print(v)" ] }, { "cell_type": "code", "execution_count": 33, "id": "197c9be2-eaf4-4d19-a908-6243ca04e7f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " ts_code trade_date open high low close pre_close change \\\n", "0 000625.SZ 20231103 15.02 15.40 14.87 15.29 15.08 0.21 \n", "1 000625.SZ 20231102 15.00 15.45 14.85 15.08 15.04 0.04 \n", "2 000625.SZ 20231101 15.18 15.31 14.81 15.04 15.13 -0.09 \n", "3 000625.SZ 20231031 15.51 15.84 14.97 15.13 15.43 -0.30 \n", "4 000625.SZ 20231030 15.79 15.79 15.16 15.43 15.81 -0.38 \n", "\n", " pct_chg vol amount \n", "0 1.3926 1436178.35 2178791.073 \n", "1 0.2660 1849642.08 2806010.330 \n", "2 -0.5948 1428837.43 2158438.838 \n", "3 -1.9443 2368677.71 3628313.928 \n", "4 -2.4035 2495361.82 3857247.322 \n" ] } ], "source": [ "# slice切片\n", "v = df.iloc[:5:1, ::]\n", "print(v)" ] }, { "cell_type": "markdown", "id": "1704a73a-be9b-44b7-b215-040e736cde81", "metadata": { "tags": [] }, "source": [ "# 2. 类的定义" ] }, { "cell_type": "code", "execution_count": 58, "id": "8dff2000-8dc0-471b-b035-b407bf765f70", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "模块注释\n", "\"\"\"\n", "class A:\n", " \"\"\"\n", " 类注释\n", " \"\"\"\n", " b = 20\n", " def __init__(self):\n", " \"\"\"\n", " 函数注释\n", " \"\"\"\n", " print(\"A\")\n", " self.a = 300 # 属性\n", " def B(self, p):\n", " print(\"函数B:\", p)\n", " print(\"成员:\",self.a, A.b)\n", " \n", " def C(p):\n", " print(\"函数C:\", p)" ] }, { "cell_type": "code", "execution_count": 59, "id": "0894aaae-da96-467f-87c5-7f4f40daf58d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A\n", "函数B: 20\n", "成员: 300 20\n", "函数C: 20\n", "300\n", "20\n" ] } ], "source": [ "# help(A)\n", "# # dir(A)\n", "a = A()\n", "a.B(20)\n", "A.C(20)\n", "print(a.a)\n", "print(A.b)" ] }, { "cell_type": "markdown", "id": "8b105e40-49f3-4441-b840-571c23f81724", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "463e8f8a-ab96-43c3-b16b-803013e4d1bf", "metadata": {}, "source": [] }, { "cell_type": "markdown", "id": "aecf7bb1-babf-4a43-94e6-dd1bd404390b", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }