跳转至


课程  因子投资  机器学习  Python  Poetry  ppw  tools  programming  Numpy  Pandas  pandas  算法  hdbscan  聚类  选股  Algo  minimum  numpy  algo  FFT  模式识别  配对交易  GBDT  LightGBM  XGBoost  statistics  CDF  KS-Test  monte-carlo  VaR  回测  过拟合  algorithms  machine learning  strategy  python  sklearn  pdf  概率  数学  面试题  量化交易  策略分类  风险管理  Info  interview  career  xgboost  PCA  wavelet  时序事件归因  SHAP  Figures  Behavioral Economics  graduate  arma  garch  人物  职场  Quantopian  figure  Banz  story  量化传奇  rsi  zigzag  穹顶压力  因子  pe  ORB  策略  Xgboost  Alligator  Indicator  factor  alpha101  alpha  技术指标  wave  quant  algorithm  pearson  spearman  tushare  因子分析  Alphalens  涨停板  herd-behaviour  因子策略  momentum  因子评估  review  SMC  聪明钱  trade  history  indicators  zscore  波动率  强化学习  顶背离  freshman  resources  others  AI  DeepSeek  network  量子计算  金融交易  IBM  weekly  LLT  backtest  backtrader  研报  papers  UBL  quantlib  jupyter-notebook  scikit-learn  pypinyin  qmt  xtquant  blog  static-site  duckdb  工具  colors  free resources  barra  world quant  Alpha  openbb  数据  risk-management  llm  prompt  CANSLIM  Augment  arsenal  copilot  vscode  code  量化数据存储  hdf5  h5py  cursor  augment  trae  Jupyter  jupysql  pyarrow  parquet  数据源  quantstats  实盘  clickhouse  notebook  redis  remote-agent  AI-tools  Moonshot  回测,研报,tushare 


“Pandas 提供了强大的日期时间处理功能,从字符串到时间戳的转换、时区调整到格式化输出,都可以轻松实现。此外,字符串操作如替换、分割、过滤等,也能通过 str 访问器高效完成。”


1. 日期和时间

1.1. ​将字符串转换为日期时间格式

如果时间或日期数据是字符串格式,可以使用 pd.to_datetime() 函数将其转换为 Pandas 的 datetime 类型。

1
2
3
4
5
6
7
8
9
import pandas as pd

# 示例数据
data = {'date': ['2023-01-01', '2023-02-01', '2023-03-01']}
df = pd.DataFrame(data)

# 将 'date' 列转换为 datetime 类型
df['date'] = pd.to_datetime(df['date'])
print(df)

输出:

1
2
3
4
        date
0 2023-01-01
1 2023-02-01
2 2023-03-01

参数说明: - format:指定日期字符串的格式,例如 '%Y-%m-%d'。


  • errors:处理错误的方式,'raise'(报错)、'coerce'(将无效值转换为 NaT)、'ignore'(保留原值)。
  • unit:指定时间单位,如 's'(秒)、'ms'(毫秒)。

1.2. ​处理多种日期格式

如果日期字符串有多种格式,可以通过 errors='coerce' 参数忽略无法解析的日期,或者使用 format 参数指定格式。

1
2
3
4
5
6
data = {'date': ['2023-01-01', '01/02/2023', 'March 3, 2023']}
df = pd.DataFrame(data)

# 处理多种日期格式
df['date'] = pd.to_datetime(df['date'], errors='coerce')
print(df)

输出:

1
2
3
4
        date
0 2023-01-01
1 2023-01-02
2 2023-03-03

1.3. ​从时间戳转换

如果数据是时间戳(如 Unix 时间戳),可以使用 pd.to_datetime() 将其转换为 datetime 类型。


示例:

1
2
3
4
5
6
data = {'timestamp': [1672531199, 1672617599, 1672703999]}
df = pd.DataFrame(data)

# 将时间戳转换为 datetime
df['date'] = pd.to_datetime(df['timestamp'], unit='s')
print(df)

输出:

1
2
3
4
   timestamp                date
0  1672531199 2023-01-01 00:00:00
1  1672617599 2023-01-02 00:00:00
2  1672703999 2023-01-03 00:00:00

1.4. ​提取日期时间信息

转换后,可以使用 dt 访问器提取日期时间的各个部分,如年、月、日、小时等。

示例:

1
2
3
4
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
df['day'] = df['date'].dt.day
print(df)

输出:

1
2
3
4
                date  year  month  day
0 2023-01-01 00:00:00  2023      1    1
1 2023-01-02 00:00:00  2023      1    2
2 2023-01-03 00:00:00  2023      1    3

1.5. ​处理时区信息

如果数据包含时区信息,可以使用 tz_convert() 和 tz_localize() 进行时区转换。

示例:

1
2
3
4
5
# 添加时区信息
df['date'] = pd.to_datetime(df['date']).dt.tz_localize('UTC')
# 转换为本地时区
df['date'] = df['date'].dt.tz_convert('Asia/Shanghai')
print(df)

1.6. ​将日期时间转换为字符串

如果需要将 datetime 类型转换为特定格式的字符串,可以使用 dt.strftime()。

示例:


1
2
df['date_str'] = df['date'].dt.strftime('%Y-%m-%d %H:%M:%S')
print(df)

输出:

1
2
3
4
                date           date_str
0 2023-01-01 08:00:00  2023-01-01 08:00:00
1 2023-01-02 08:00:00  2023-01-02 08:00:00
2 2023-01-03 08:00:00  2023-01-03 08:00:00

2. 字符串操作

2.1. DataFrame 的字符串操作

在 Pandas 中,DataFrame 的字符串操作可以通过 str 访问器来实现。以下是一些常见的字符串操作方法:

2.1.1. 转换为大写或小写

1
2
df['column_name'] = df['column_name'].str.upper()  # 转换为大写
df['column_name'] = df['column_name'].str.lower()  # 转换为小写

2.1.2. 替换子字符串


1
df['column_name'] = df['column_name'].str.replace('old', 'new')  # 替换子字符串

2.1.3. 提取子字符串

1
df['new_column'] = df['column_name'].str[:3]  # 提取前 3 个字符

2.1.4. 分割字符串

1
df[['part1', 'part2']] = df['column_name'].str.split(' ', expand=True)  # 按空格分割

2.1.5. 检查是否包含子字符串

1
df['contains_substring'] = df['column_name'].str.contains('substring')  # 检查是否包含

2.1.6. 计算字符串长度

1
df['length'] = df['column_name'].str.len()  # 计算字符串长度

2.1.7. 去除空格

1
df['column_name'] = df['column_name'].str.strip()  # 去除两端空格

2.1.8. 正则表达式匹配

1
df['matches'] = df['column_name'].str.contains(r'\d')  # 检查是否包含数字

2.2. 排除科创板证券

科创板证券的代码通常以 688 开头。假设 DataFrame 中有一列 code 存放证券代码,可以通过以下方法排除科创板证券:

方法 1:使用 ~ 和 str.startswith()

1
df_filtered = df[~df['code'].str.startswith('688')]

方法 2:使用 str.contains() 和正则表达式

1
df_filtered = df[~df['code'].str.contains(r'^688')]

方法 3:使用 query() 方法

1
df_filtered = df.query("not code.str.startswith('688')", engine='python')

示例:

1
2
3
4
5
6
7
8
9
import pandas as pd

# 示例数据
data = {'code': ['600001', '688001', '000001', '688002'], 'name': ['A', 'B', 'C', 'D']}
df = pd.DataFrame(data)

# 排除科创板
df_filtered = df[~df['code'].str.startswith('688')]
print(df_filtered)

输出:

1
2
3
     code name
0  600001    A
2  000001    C

Notes

总结 - 字符串操作:通过 str 访问器可以实现大小写转换、替换、提取、分割、检查等操作。 - 排除科创板:使用 str.startswith() 或正则表达式过滤掉以 688 开头的证券代码。