Python:降低精度 pandas 时间戳数据框 [英] Python: reduce precision pandas timestamp dataframe

查看:72
本文介绍了Python:降低精度 pandas 时间戳数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有以下数据框

df = 

       Record_ID       Time
        94704   2014-03-10 07:19:19.647342
        94705   2014-03-10 07:21:44.479363
        94706   2014-03-10 07:21:45.479581
        94707   2014-03-10 07:21:54.481588
        94708   2014-03-10 07:21:55.481804

有可能有以下人吗?

df1 = 

       Record_ID       Time
        94704   2014-03-10 07:19:19
        94705   2014-03-10 07:21:44
        94706   2014-03-10 07:21:45
        94707   2014-03-10 07:21:54
        94708   2014-03-10 07:21:55

推荐答案

您可以转换基础astype将="noreferrer"> datetime64[ns] 值转换为datetime64[s]值:

You could convert the underlying datetime64[ns] values to datetime64[s] values using astype:

In [11]: df['Time'] = df['Time'].astype('datetime64[s]')

In [12]: df
Out[12]: 
   Record_ID                Time
0      94704 2014-03-10 07:19:19
1      94705 2014-03-10 07:21:44
2      94706 2014-03-10 07:21:45
3      94707 2014-03-10 07:21:54
4      94708 2014-03-10 07:21:55

请注意,由于Pandas Series和DataFrames 将所有日期时间值存储为datetime64[ns] ,因此datetime64[s]值会自动转换回datetime64[ns],因此最终结果仍会存储为datetime64[ns]值,但是调用astype会导致秒的小数部分被删除.

Note that since Pandas Series and DataFrames store all datetime values as datetime64[ns] these datetime64[s] values are automatically converted back to datetime64[ns], so the end result is still stored as datetime64[ns] values, but the call to astype causes the fractional part of the seconds to be removed.

如果您希望拥有一个datetime64[s]值的NumPy数组,则可以使用df['Time'].values.astype('datetime64[s]').

If you wish to have a NumPy array of datetime64[s] values, you could use df['Time'].values.astype('datetime64[s]').

这篇关于Python:降低精度 pandas 时间戳数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆