由于數(shù)據(jù)更新的需要俺附,往往需要將新日期獲取的數(shù)據(jù)合并到原有的數(shù)據(jù)中英融,pandas中的concat函數(shù)能很好的完成合并工作棘伴,再通過使用drop_duplicates方法去除重復的數(shù)據(jù)即可趁耗。
實際運用中通常獲取最后的更新日期,從該日期后進行查詢大脉,可以使用day+1的方法規(guī)避重復數(shù)據(jù)搞监,也可以應用上述方法,去除重復
# -*- 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))