由于數據更新的需要,往往需要將新日期獲取的數據合并到原有的數據中,pandas中的concat函數能很好的完成合并工作,再通過使用drop_duplicates方法去除重復的數據即可。
實際運用中通常獲取最后的更新日期,從該日期后進行查詢,可以使用day+1的方法規避重復數據,也可以應用上述方法,去除重復
# -*- coding: utf-8 -*-
import pandas as pd
import tushare as ts
#獲取第一個df
df1 = ts.get_k_data('510050', start='2014-02-21',end='2014-04-01',ktype='D',autype='qfq')
#重構索引
df1.set_index(['date'], inplace = True)
#獲取第二個df
df2 = ts.get_k_data('510050', start='2014-01-01',end='2014-04-01',ktype='D',autype='qfq')
#重構索引
df2.set_index(['date'], inplace = True)
#兩個dataframe合并
df_new=pd.concat([df1, df2])
#檢查去重
df_new = df_new.drop_duplicates()
#按照索引[日期]進行排序,升序
print(df_new.sort_index(ascending = True))