How to resolve TypeError: cannot convert the series to <type 'float'>
How to resolve TypeError: cannot convert the series to <type 'float'>
对于特定的纪元值,时间的纪元值转换如下:
time.strftime("%H:%M:%S ", time.localtime(28000000000))
Out[82]: '07:16:40 '
但是当我将上述方法应用于数据集中的纪元时间列时,
time.strftime("%H:%M:%S ", time.localtime(df1['d']))
我收到以下错误:
TypeError: cannot convert the series to type 'float'
我哪里错了?
df1['d']
为纪元持续时间,列中数据如下:
28000000000
16000000000
33000000000
28000000000
27000000000
22000000000
26000000000
22000000000
22000000000
22000000000
46000000000
我需要纪元时间而不是 datetime
对象格式。
我认为需要 Series.apply
和 lambda 函数:
df1 = pd.DataFrame({'d':[28000000000,28000000000]})
df1['times'] = df1['d'].apply(lambda x: time.strftime("%H:%M:%S", time.localtime(x)))
或list comprehension
:
df1['times'] = [time.strftime("%H:%M:%S", time.localtime(x)) for x in df1['d']]
print (df1)
d times
0 28000000000 03:46:40
1 28000000000 03:46:40
您可以使用map
函数。
import pandas as pd
import time
df = pd.DataFrame([[28000000000, 2.5], [28100000000, 2.54]], columns=['d', 'e'])
df['time'] = df.d.map(lambda t: time.strftime("%H:%M:%S ", time.localtime(t)))
print(df)
# d e time
# 0 28000000000 2.50 03:46:40
# 1 28100000000 2.54 13:33:20
对于特定的纪元值,时间的纪元值转换如下:
time.strftime("%H:%M:%S ", time.localtime(28000000000))
Out[82]: '07:16:40 '
但是当我将上述方法应用于数据集中的纪元时间列时,
time.strftime("%H:%M:%S ", time.localtime(df1['d']))
我收到以下错误:
TypeError: cannot convert the series to type 'float'
我哪里错了?
df1['d']
为纪元持续时间,列中数据如下:
28000000000
16000000000
33000000000
28000000000
27000000000
22000000000
26000000000
22000000000
22000000000
22000000000
46000000000
我需要纪元时间而不是 datetime
对象格式。
我认为需要 Series.apply
和 lambda 函数:
df1 = pd.DataFrame({'d':[28000000000,28000000000]})
df1['times'] = df1['d'].apply(lambda x: time.strftime("%H:%M:%S", time.localtime(x)))
或list comprehension
:
df1['times'] = [time.strftime("%H:%M:%S", time.localtime(x)) for x in df1['d']]
print (df1)
d times
0 28000000000 03:46:40
1 28000000000 03:46:40
您可以使用map
函数。
import pandas as pd
import time
df = pd.DataFrame([[28000000000, 2.5], [28100000000, 2.54]], columns=['d', 'e'])
df['time'] = df.d.map(lambda t: time.strftime("%H:%M:%S ", time.localtime(t)))
print(df)
# d e time
# 0 28000000000 2.50 03:46:40
# 1 28100000000 2.54 13:33:20