Tushare Day4——导入IPO新股列表new_share并分析基金和盈利

IPO新股列表new_share

1. 基础信息

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import pandas as pd
import tushare as ts
from sqlalchemy import create_engine 
import pymysql
pymysql.install_as_MySQLdb()
date_1 = '19600101'
date_2 = '19800101'
### 在2000年之前没有数据
date_3 = '20000101'
date_4 = '20150101'
date_5 = '20210101'

2. 从数据接口取出new_share(由于限制,分两次取再合并)

2.1 20000101到20150101

# 由于每次限制取2000条,这里分两次取出来
pro = ts.pro_api()

df = pro.new_share(start_date = date_3, end_date = date_4)
df.head()
ts_code sub_code name ipo_date issue_date amount market_amount price pe limit_amount funds ballot
0 603889.SH 732889 新澳股份 20141223 20141231 2668.0 2401.2 17.95 21.63 1.0 4.7891 0.43
1 603017.SH 732017 园区设计 20141222 20141231 1500.0 1350.0 29.97 22.88 0.6 4.4955 0.55
2 603636.SH 732636 南威软件 20141222 20141230 2500.0 2250.0 14.95 22.96 1.0 3.7375 0.40
3 002736.SZ 002736 国信证券 20141219 20141229 120000.0 60000.0 5.83 22.97 36.0 69.9600 1.80
4 002738.SZ 002738 中矿资源 20141219 20141230 3000.0 2700.0 7.57 22.98 1.2 2.2710 0.40
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1082 entries, 0 to 1081
Data columns (total 12 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   ts_code        1082 non-null   object 
 1   sub_code       1082 non-null   object 
 2   name           1082 non-null   object 
 3   ipo_date       1082 non-null   object 
 4   issue_date     1082 non-null   object 
 5   amount         1082 non-null   float64
 6   market_amount  1082 non-null   float64
 7   price          1082 non-null   float64
 8   pe             1082 non-null   float64
 9   limit_amount   1082 non-null   float64
 10  funds          1082 non-null   float64
 11  ballot         1082 non-null   float64
dtypes: float64(7), object(5)
memory usage: 101.6+ KB

2.2 20150101到20210101

pro = ts.pro_api()

df_2 = pro.new_share(start_date = date_4, end_date = date_5)
df_2.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1369 entries, 0 to 1368
Data columns (total 12 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   ts_code        1369 non-null   object 
 1   
这个 SQL 条件 `DATE_TRUNC('day', new_sign_out_time) = DATE_TRUNC('day', new_sign_in_time)` 用于比较两个时间戳(`new_sign_out_time` `new_sign_in_time`)是否属于**同一天**,忽略具体的时间部分(时、分、秒)。 ### **作用解析** 1. **`DATE_TRUNC('day', timestamp)`** - 这是 PostgreSQL 的函数,用于将时间戳截断到指定的精度(这里是 `'day'`)。 - 例如: - `DATE_TRUNC('day', '2023-10-01 14:30:00')` → `2023-10-01 00:00:00` - `DATE_TRUNC('day', '2023-10-02 08:15:00')` → `2023-10-02 00:00:00` 2. **`=` 比较** - 如果 `new_sign_out_time` `new_sign_in_time` 截断到天后的结果相同,说明它们在同一天。 - 例如: - `new_sign_in_time = '2023-10-01 09:00:00'` - `new_sign_out_time = '2023-10-01 18:00:00'` - 条件成立(同一天)。 ### **适用场景** - 检查“签入”“签出”是否发生在同一天(如考勤系统、工时统计)。 - 筛选某一天内的记录(如日志分析、报表生成)。 ### **其他数据库的替代写法** - **MySQL / MariaDB**: ```sql DATE(new_sign_out_time) = DATE(new_sign_in_time) ``` - **SQL Server**: ```sql CONVERT(DATE, new_sign_out_time) = CONVERT(DATE, new_sign_in_time) ``` - **Oracle**: ```sql TRUNC(new_sign_out_time) = TRUNC(new_sign_in_time) ``` ### **性能优化建议** 1. **避免在列上使用函数**(可能导致索引失效): - 反例:`DATE_TRUNC('day', new_sign_out_time) = ...`(可能无法用索引)。 - 优化:改用范围查询(如果列有索引): ```sql new_sign_out_time >= DATE_TRUNC('day', new_sign_in_time) AND new_sign_out_time < DATE_TRUNC('day', new_sign_in_time) + INTERVAL '1 day' ``` 2. **如果频繁查询同一天的数据**,可考虑存储一个冗余的 `date` 列(如 `sign_in_date`)。 ### **常见问题** 1. **如果 `new_sign_out_time` 为 NULL**,条件会如何? - 结果为 `NULL`(不满足 `=` 比较),需额外处理: ```sql (DATE_TRUNC('day', new_sign_out_time) = DATE_TRUNC('day', new_sign_in_time) OR new_sign_out_time IS NULL) ``` 2. **如何扩展到“同一周”或“同一月”**? - 替换 `'day'` 为 `'week'` 或 `'month'`: ```sql DATE_TRUNC('week', new_sign_out_time) = DATE_TRUNC('week', new_sign_in_time) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值