pd.to_datetime(date).strftime("%Y%m%d")
时间: 2024-04-27 13:23:50 浏览: 31
这是一个Python pandas库中的函数,它将日期或日期字符串转换为指定格式的字符串。具体地,它将一个日期对象或日期字符串转换为年月日(%Y%m%d)的字符串格式。其中,%Y代表四位数的年份,%m代表两位数的月份,%d代表两位数的日期。例如,如果date为"2022-01-01",则该函数将返回"20220101"。
相关问题
data=pd.read_csv('mock_kaggle.csv',encoding ='gbk',parse_dates=['datetime']) Date=pd.to_datetime(data.datetime) data['date'] = Date.map(lambda x: x.strftime('%Y-%m-%d')) datanew=data.set_index(Date) series = pd.Series(datanew['股票'].values, index=datanew['date'])
这段代码的功能是将一个名为`mock_kaggle.csv`的CSV文件读取为Pandas DataFrame,然后将日期时间列解析为日期时间格式,并将其设置为索引。接着,使用`map()`函数将日期时间格式化为`%Y-%m-%d`的字符串格式,并将其存储在名为`date`的新列中。最后,将数据的股票信息存储在一个名为`series`的Pandas Series中,其中日期为索引,股票信息为值。
解释如下:
1. `data=pd.read_csv('mock_kaggle.csv',encoding ='gbk',parse_dates=['datetime'])`
- 使用`pd.read_csv()`函数读取名为`mock_kaggle.csv`的CSV文件,并将其解析为Pandas DataFrame。
- `encoding ='gbk'`指定使用GBK编码方式读取文件。
- `parse_dates=['datetime']`将`datetime`列解析为日期时间格式。
2. `Date=pd.to_datetime(data.datetime)`
- 使用`pd.to_datetime()`函数将`datetime`列转换为Pandas的日期时间格式。
- 将转换后的结果存储在名为`Date`的变量中。
3. `data['date'] = Date.map(lambda x: x.strftime('%Y-%m-%d'))`
- 使用`map()`函数将`Date`变量中的日期时间格式转换为`%Y-%m-%d`的字符串格式。
- 将转换后的结果存储在名为`date`的新列中。
4. `datanew=data.set_index(Date)`
- 使用`set_index()`函数将`Date`变量设置为数据的索引。
- 将结果存储在名为`datanew`的新DataFrame中。
5. `series = pd.Series(datanew['股票'].values, index=datanew['date'])`
- 使用`pd.Series()`函数将`datanew` DataFrame中的股票信息存储在名为`series`的Pandas Series中。
- 将`date`列作为索引,`股票`列的值作为Pandas Series的值。
def __stk_surv(): df=pd.read_sql("select ts_code from stock_basic", con = con0) ts_code = (df[df['ts_code'].str.contains('[HZ]')]['ts_code'] .drop_duplicates() .apply(lambda x: f"'{x}'") .reset_index(drop=True)) for i in ts_code: try: start_date = str(pd.read_sql("select surv_date from stk_surv", con = con3).sort_values('surv_date',ascending = False).iloc[0,0]) start_date=pd.to_datetime(start_date)+pd.Timedelta(1,'days') start_date=start_date.strftime('%Y%m%d') except: start_date = 20220101 dateToday = datetime.date.today().strftime('%Y%m%d') end_date = dateToday i = i[1:10] df = pro.stk_surv(**{ "ts_code": i, "trade_date": "", "start_date": start_date, "end_date": end_date, "limit": "", "offset": "" }, fields=[ "ts_code", "name", "surv_date", "fund_visitors", "rece_place", "rece_mode", "rece_org", "org_type", "comp_rece" ]) # print(df) time.sleep(0.31) df = df[df['rece_mode'].str.contains('调研')] df.to_sql(name='stk_surv',con=con3,if_exists='append',index=False) 增加进度条
你可以在循环中使用tqdm库来添加进度条。以下是修改后的代码示例:
```
from tqdm import tqdm
import time
def __stk_surv():
df=pd.read_sql("select ts_code from stock_basic", con = con0)
ts_code = (df[df['ts_code'].str.contains('[HZ]')]['ts_code']
.drop_duplicates()
.apply(lambda x: f"'{x}'")
.reset_index(drop=True))
for i in tqdm(ts_code):
try:
start_date = str(pd.read_sql("select surv_date from stk_surv", con = con3).sort_values('surv_date',ascending = False).iloc[0,0])
start_date=pd.to_datetime(start_date)+pd.Timedelta(1,'days')
start_date=start_date.strftime('%Y%m%d')
except:
start_date = 20220101
dateToday = datetime.date.today().strftime('%Y%m%d')
end_date = dateToday
i = i[1:10]
df = pro.stk_surv(**{
"ts_code": i,
"trade_date": "",
"start_date": start_date,
"end_date": end_date,
"limit": "",
"offset": ""
}, fields=[
"ts_code",
"name",
"surv_date",
"fund_visitors",
"rece_place",
"rece_mode",
"rece_org",
"org_type",
"comp_rece"
])
# print(df)
time.sleep(0.31)
df = df[df['rece_mode'].str.contains('调研')]
df.to_sql(name='stk_surv',con=con3,if_exists='append',index=False)
```
在上面的代码中,我们使用了tqdm库来包装循环,以自动为你添加进度条。你可以根据需要自定义进度条的样式和位置。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)