从纪元时间到日期时间对象系列,有没有快速的方法来转换一系列秒数?
我用:
for i in range(len(df)):
df['datetime'].iloc[i] = datetime.fromtimestamp(df['epochtime'].iloc[i])
但由于我的数据帧非常大,所以速度很慢。有没有快速的方法来做到这一点?像熊猫的功能?
答案 0 :(得分:2)
您可以使用to_datetime(..., unit='s')
:
df['datetime'] = pd.to_datetime(df['epochtime'], unit='s')
时间比较:
In [158]: df = pd.DataFrame({'epochtime': pd.date_range('2001-01-01', freq='1S', periods=10**5)}).astype(np.int64)//10**9
In [159]:
In [159]: df.head()
Out[159]:
epochtime
0 978307200
1 978307201
2 978307202
3 978307203
4 978307204
In [160]:
In [160]: len(df)
Out[160]: 100000
In [161]:
In [161]: %timeit df['datetime'] = pd.to_datetime(df['epochtime'], unit='s')
100 loops, best of 3: 16.9 ms per loop
In [162]:
In [162]: %%timeit
.....: for i in range(len(df)):
.....: df['datetime'].iloc[i] = datetime.fromtimestamp(df2['epochtime'].iloc[i])
.....:
c:\envs\py35\lib\site-packages\pandas\core\indexing.py:128: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
1 loop, best of 3: 54.5 s per loop
<强>结论:强>
@Natecat,
您可以看到16.9 ms
与54.5 s