我有 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”有什么建議嗎?
uj5u.com熱心網友回復:
用于Series.dt.floor洗掉毫秒和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]
uj5u.com熱心網友回復:
采用:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/454564.html
上一篇:如何從復雜的json制作資料框?
