Upgrade TuShare to the latest version
masterTo upgrade your existing TuShare installation to the latest version, use the --upgrade flag with pip.
pip install tushare --upgraderepository·master·Indexed 11 days ago
https://github.com/waditu/tushareA tool for the collection, cleaning, and storage of financial data, including stocks and futures, designed for quantitative analysts and data science learners. It provides interfaces for retrieving historical transaction data via get_hist_data(), real-time quotes, Dragon-Tiger List statistics, and movie box office data. The library also supports industry, concept, and area classifications, as well as index constituents for CSI 300, SSE 50, and CSI 500.
To upgrade your existing TuShare installation to the latest version, use the --upgrade flag with pip.
pip install tushare --upgradeUse the to_json method to generate JSON files or strings.
Common Parameters:
path_or_buf: Path to the JSON file.orient: JSON format orientation (e.g., 'columns', 'records', 'index', 'split', 'values'). Default is 'columns'.force_ascii: Whether to convert characters to ASCII (default is True).import tushare as ts
df = ts.get_hist_data('000875')
# Save to file with 'records' orientation
df.to_json('c:/day/000875.json', orient='records')
# Or print as JSON string
print(df.to_json(orient='records'))Use the to_excel method to save data to Microsoft Excel format.
Common Parameters:
excel_writer: File path or ExcelWriter object.sheet_name: Name of the sheet (default is Sheet1).startrow: Number of empty rows to leave at the top.startcol: Number of empty columns to leave on the left.import tushare as ts
df = ts.get_hist_data('000875')
# Direct save
df.to_excel('c:/day/000875.xlsx')
# Set data position (starting from 3rd row, 6th column)
df.to_excel('c:/day/000875.xlsx', startrow=2, startcol=5)Use the to_hdf method to save data in HDF5 format. This requires the PyTables package (version >= 3.0.0).
Common Parameters:
path_or_buf: File path or HDFStore object.key: Group identifier within the HDF5 file.mode: 'a' (append/read-write, default), 'w' (write), 'r' (read), or 'r+' (read/write, file must exist).format: 'fixed' (fast read/write, no append) or 'table' (supports searching and appending).append: Set to True if using format='table' to append data.complevel: Compression level (1-9, default 0).complib: Compression library (e.g., 'zlib', 'bzip2', 'lzo', 'blosc').import tushare as ts
from pandas import HDFStore
df = ts.get_hist_data('000875')
# Method 1: Direct save
df.to_hdf('c:/day/hdf.h5', '000875')
# Method 2: Using HDFStore object
store = HDFStore('c:/day/store.h5')
store['000875'] = df
store.close()You can save tushare data (as pandas DataFrame or Series objects) to CSV files using the to_csv method.
Common Parameters:
path_or_buf: File path or StringIO object.sep: Delimiter (default is ,).na_rep: Character to use for NaN values (default is '').columns: List of columns to save.header: Whether to save column names (default is True).index: Whether to save the index (default is True).mode: 'w' for new file (default) or 'a' for appending.encoding: File encoding.Note: Ensure the target directory exists before saving, otherwise an IOError will be raised.
import tushare as ts
df = ts.get_hist_data('000875')
# Direct save
df.to_csv('c:/day/000875.csv')
# Save specific columns
df.to_csv('c:/day/000875.csv', columns=['open', 'high', 'low', 'close'])To upgrade your existing TuShare installation to the latest version, use the --upgrade flag with pip:
pip install tushare --upgradepip install tushare --upgradeTo use DataYes (通联数据) interfaces through TuShare, you must first register an account on the DataYes website and obtain a token. Once you have your token, you need to configure it in your Python environment using ts.set_token(). This only needs to be done once unless you regenerate your token.
Steps to get a token:
To verify your current token, use ts.get_token().
import tushare as ts
# Set your DataYes token
ts.set_token('xxxxxxxxxxxxxxxxxxxxxxxxxxxx')
# Verify the token
print(ts.get_token())Pandas does not have a direct to_mongodb method. To save data to MongoDB, convert the DataFrame to a JSON string with orient='records' and then insert it using pymongo.
import tushare as ts
import pymongo
import json
# Connect to MongoDB
conn = pymongo.MongoClient('127.0.0.1', port=27017)
# Note: In newer pymongo, use MongoClient instead of Connection
df = ts.get_tick_data('600848', date='2014-12-22')
# Convert to JSON records and insert
conn.db.tickdata.insert_many(json.loads(df.to_json(orient='records')))If you wish to support the development of TuShare, you can provide financial support via WeChat or Alipay. In return, the author offers assistance with programming languages, databases, Python/pandas, or quantitative analysis questions.
**捐助方式一(微信)**
请通过微信“扫一扫”下面的二维码(或添加我的微信jimmysoa打赏个红包也OK:-)
**捐助方式二(支付宝)**
请打开支付宝App“扫一扫”下面的二维码(或通过帐号liuzhiming@ymail.com)You can install TuShare using pip or by downloading it directly from PyPI.
Before installing, ensure you have the following installed:
pip install lxml)It is highly recommended to use Anaconda to manage your Python environment and dependencies to minimize installation issues.
pip install tushareYou can save data to relational databases (MySQL, PostgreSQL, Oracle, MS SQLServer, SQLite) using the to_sql method. It is recommended to use sqlalchemy to create the connection engine.
Common Parameters:
name: Table name (pandas will automatically create the table structure).con: Database connection (use a sqlalchemy engine).if_exists: How to handle existing tables: 'fail' (default), 'replace', or 'append'.index: Whether to store the pandas Index as a column (default is True).index_label: Column name for the Index.chunksize: Number of rows to write at a time (default is None, writes all at once).dtype: Dictionary specifying column data types.from sqlalchemy import create_engine
import tushare as ts
df = ts.get_tick_data('600848', date='2014-12-22')
engine = create_engine('mysql://user:passwd@127.0.0.1/db_name?charset=utf8')
# Save to database
df.to_sql('tick_data', engine)
# Append to existing table
# df.to_sql('tick_data', engine, if_exists='append')You can install TuShare using the standard Python package manager pip.
pip install tushare