Convert pandas dataframe column of UTC time string to floats

瘦欲@ 提交于 2021-01-28 10:38:11

问题


I have a pandas dataframe with a column of strings, with datetimes in UTC format, but need to convert them to floats. I'm having trouble doing this. Here is a view of my column:

df['time'][0:3]

0    2018-04-18T19:00:00.000000000Z
1    2018-04-18T19:15:00.000000000Z
2    2018-04-18T19:30:00.000000000Z
Name: time, dtype: object

I've been trying this, but isn't working for me:

import datetime
for i in range(1,len(df)):
        df['time'][i] = datetime.datetime.strptime(df['time'][i], '%Y-%m-%dT%H:%M:%S.%f000Z')

Here is the error I'm trying to fix:

execfile(filename, namespace)

exec(compile(f.read(), filename, 'exec'), namespace)

unsup.fit(np.reshape(df,(-1,df.shape[1])))

X = _check_X(X, self.n_components)

X = check_array(X, dtype=[np.float64, np.float32])

array = np.array(array, dtype=dtype, order=order, copy=copy)

ValueError: could not convert string to float: '2018-06-29T20:45:00.000000000Z'

Many thanks in advance.


回答1:


I think you can use to_datetime with parameter format:

df['time1'] = pd.to_datetime(df['time'], format='%Y-%m-%dT%H:%M:%S.%f000Z')
print (df)
                             time               time1
0  2018-04-18T19:00:00.000000000Z 2018-04-18 19:00:00
1  2018-04-18T19:15:00.000000000Z 2018-04-18 19:15:00
2  2018-04-18T19:30:00.000000000Z 2018-04-18 19:30:00

For assign back:

df['time'] = pd.to_datetime(df['time'], format='%Y-%m-%dT%H:%M:%S.%f000Z')
print (df)
                 time
0 2018-04-18 19:00:00
1 2018-04-18 19:15:00
2 2018-04-18 19:30:00


来源:https://stackoverflow.com/questions/51112320/convert-pandas-dataframe-column-of-utc-time-string-to-floats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!