Home > Net >  influx result set to datetime column pandas
influx result set to datetime column pandas

Time:04-01

i have influxdb result set as

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})

Im converting it into dataframe by

df = pd.DataFrame(results2)

but the time format is coming as 2022-03-22T16:37:39.643127Z

i want it to be as 2022-03-22 16:37:39

Tried with

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

but its not as i mentioned. its showing as "2022-03-22 16:37:39.643127 00:00 " Any suggestions ?

CodePudding user response:

Use Series.dt.floor for remove miliseconds with Series.dt.tz_convert for remove timezones:

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]

Only removed timezones:

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]

CodePudding user response:

Use:

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

Output:

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
  • Related