Skip to main content

A lightweight Python utility to convert A-share stock codes to various formats

Project description

A-Share Code Converter

a-share-code-converter is a lightweight Python utility that helps you normalize and convert Chinese A-share stock codes between various formats. It supports:

  • Conversion to pure integer codes (e.g., 600519)
  • Conversion to zero-padded string codes (e.g., "600519")
  • Conversion to suffixed formats indicating exchanges (e.g., "600519.SH")

The converter supports multiple input types, including:

  • Python list
  • NumPy arrays
  • pandas.Series
  • polars.Series

📦 Installation

pip install a-share-code-converter

🚀 Usage

The CodeConverter class provides the following key methods:

1. CodeConverter.to_int(codes)

Converts various code formats (e.g. "600519.SH", "000001", 430047) into pure 6-digit integers.

  • ✅ Strips suffixes like .SH, .SZ, .BJ
  • ✅ Works with list, numpy.ndarray, pandas.Series, polars.Series
from a_share_code_converter import CodeConverter

codes = ["600519", "000001.SZ", "430047.BJ", 2415]

CodeConverter.to_int(codes)
# Output: [600519, 1, 430047, 2415]

2. CodeConverter.to_str(codes)

Converts input to zero-padded 6-digit string format without any suffix.

✅ Ensures all codes are 6-character strings like '000001', '600519'

CodeConverter.to_str(codes)
# Output: ['600519', '000001', '430047', '002415']

3. CodeConverter.to_suffix(codes)

Converts codes to full format with market suffix, e.g., '600519.SH'.

  • ✅ Based on numeric code ranges:

    • .SZ: 000000–399999

    • .SH: 600000–699999

    • .BJ: 400000–499999, 800000–899999

CodeConverter.to_suffix(codes)
# Output: ['600519.SH', '000001.SZ', '430047.BJ', '002415.SZ']

4. CodeConverter.convert(codes, to)

A unified entry point to perform conversion. Choose target format via to parameter:

to value Result
'int' Integer codes (int or np.int64)
'str' 6-digit zero-padded string codes
'suffix' 6-digit string with .SH/.SZ/.BJ
CodeConverter.convert(codes, to='int')
# Output: [600519, 1, 430047, 2415]

CodeConverter.convert(codes, to='str')
# Output: ['600519', '000001', '430047', '002415']

CodeConverter.convert(codes, to='suffix')
# Output: ['600519.SH', '000001.SZ', '430047.BJ', '002415.SZ']

🧪 Supported Input Types

  • list[str | int | float]

  • numpy.ndarray

  • pandas.Series

  • polars.Series

All methods will preserve the original type (e.g., return a pandas.Series if input is a pandas.Series).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a-share-code-converter-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

a_share_code_converter-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file a-share-code-converter-0.1.0.tar.gz.

File metadata

  • Download URL: a-share-code-converter-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for a-share-code-converter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1474c4e95f7fd55724f19c26a8701049ba27783cd7b427a86e7e27f2e04f7d4a
MD5 ca5400cbcebb32b9eea552251998f0d5
BLAKE2b-256 38261661369c17caa75a70f1a38d2dfb19286166195064301626ae4c3e477bef

See more details on using hashes here.

File details

Details for the file a_share_code_converter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for a_share_code_converter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 806dc82ba3a0366225ccd46d544c5a8f5ea7c8130cb63db4440459b41931fbe7
MD5 5a683d62c6920c92ff6787fcef1fc373
BLAKE2b-256 d5fb2aaa9ccd1a4801582fc7cef323aee6aa94ea3f968d8569a1ab5a8684e863

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page