You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python Pandas Timestamp转换为重复的google.protobuf.Timestamp?

将Pandas DataFrame转换为Protobuf序列化对象的问题

我正在编写代码,要把Pandas DataFrame的内容转换为可序列化并写入文件的Protobuf对象,相关细节及遇到的问题如下:

Protobuf定义与编译

Protobuf结构定义:

syntax = "proto3";

import "google/protobuf/timestamp.proto";

message BidAskTimeseries {
    repeated double bid = 1;
    repeated double ask = 2;
    repeated google.protobuf.Timestamp timestamp = 3;
}

编译命令:

protoc --proto_path=. --python_out=. bid_ask_timeseries.proto

DataFrame数据准备

从CSV加载DataFrame:

import pandas

df = pandas.read_csv('df.csv')

DataFrame的数据类型:

df_data.dtypes
ask    float64
bid    float64
ts      object

其中ts列实际为字符串类型,可通过以下代码转换为Pandas Timestamp类型(这一步因代码其他需求更合适):

df['ts'] = pandas.to_datetime(df['ts'])

遇到的错误

直接使用字符串时间列的错误

原本想保留ts为字符串类型,利用Timestamp.FromString方法转换,但执行以下代码时报错:

import bid_ask_timeseries_pb2
from google.protobuf.timestamp import Timestamp

bid_ask_timeseries = bid_ask_timeseries_pb2.BidAskTimeseries(
    bid=df['bid'],
    ask=df['ask'],
    timestamp=df['ts'],
)

错误信息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected bytes, str found

转换为Pandas Timestamp后的错误

将ts转为datetime类型后,尝试通过FromDatetime方法逐个转换为Protobuf的Timestamp对象,再构造Protobuf实例:

def convert(t):
    timestamp = Timestamp()
    timestamp.FromDatetime(t)
    return timestamp

timestamp_list = list(map(convert, df['ts']))

bid_ask_timeseries = bid_ask_timeseries_pb2.BidAskTimeseries(
    bid=df['bid'],
    ask=df['ask'],
    timestamp=timestamp_list,
)

执行时出现错误:

TypeError: 'Timestamp' object cannot be interpreted as an integer

尝试将列表转为numpy array,错误依旧。我无法理解:明明Protobuf定义里timestamp是repeated google.protobuf.Timestamp类型,为什么构造函数会期望整数类型?


内容的提问来源于stack exchange,提问作者user2138149

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 07:05:59