涌入结果集到日期时间列 pandas

influx result set to datetime column pandas

我将 influxdb 结果设置为

results2 = ResultSet({'('options_price_reference_limits', None)': [{'time': '2022-03-22T16:37:39.643127Z', 'lower_boundary': -439.8286562572736, 'mark_price': 144.060885, 'symbol': 'C-BTC-44000-250322', 'upper_boundary': 556.8514272321878}, {'time': '2022-03-22T16:37:44.671990Z', 'lower_boundary': -445.06319445515476, 'mark_price': 144.615283, 'symbol': 'C-BTC-44000-250322', 'upper_boundary': 563.4589659718127}, {'time': '2022-03-22T16:37:49.715605Z', 'lower_boundary': -441.5881618331249, 'mark_price': 144.688937, 'symbol': 'C-BTC-44000-250322', 'upper_boundary': 559.0723733380117}, {'time': '2022-03-22T16:37:54.792814Z', 'lower_boundary': -455.1554628099913, 'mark_price': 146.319068, 'symbol': 'C-BTC-44000-250322', 'upper_boundary': 576.1976471885248})

我通过

将其转换为数据帧
df = pd.DataFrame(results2)

但时间格式即将变为 2022-03-22T16:37:39.643127Z

我希望它像 2022-03-22 16:37:39

尝试过

df['time2']=pd.to_datetime(df['time'])

但它不像我提到的那样。它显示为“2022-03-22 16:37:39.643127+00:00 “ 有什么建议吗?

使用:

pd.to_datetime(df['time']).dt.strftime('%Y-%m-%d %H:%M:%S')

输出:

0    2022-03-22 16:37:39
1    2022-03-22 16:37:44
2    2022-03-22 16:37:49
3    2022-03-22 16:37:54
Name: time, dtype: object

使用 Series.dt.floor for remove miliseconds with Series.dt.tz_convert 删除时区:

A = ['2022-03-22T16:37:39.643127Z','2022-03-22T16:37:39.643127Z']
df = pd.DataFrame(A,columns = ['time'])

print (pd.to_datetime(df['time']).dt.floor('S').dt.tz_convert(None))
0   2022-03-22 16:37:39
1   2022-03-22 16:37:39
Name: time, dtype: datetime64[ns]

仅删除时区:

print (pd.to_datetime(df['time']).dt.tz_convert(None))
0   2022-03-22 16:37:39.643127
1   2022-03-22 16:37:39.643127
Name: time, dtype: datetime64[ns]