Python:减少精度 Pandas 时间戳数据帧

标签 python pandas timestamp dataframe

你好,我有以下数据框

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

最佳答案

您可以转换底层 datetime64[ns]使用 astype 将值转换为 datetime64[s] 值:

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 store all datetime values as datetime64[ns]这些 datetime64[s] 值会自动转换回 datetime64[ns],因此最终结果仍存储为 datetime64[ns] 值,但对 astype 的调用导致秒的小数部分被删除。

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

关于Python:减少精度 Pandas 时间戳数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32827169/

相关文章:

mysql - 时间戳格式 dd/mm/yyyy hh :mm:ss to yyyy-mm-dd hh:mm:ss - MySQL

python - 使用 Tornado 将二进制文件流式传输到 Google Storage

php - 数据库之间的重复值

python - 在 Windows 中的 Python 3.6 中安装 mysqlclient

python - 在 numpy 矩阵中交换零

datetime - JMeter - 如何将人类可读的日期时间转换为 Unix 时间戳

python selenium,找出下载何时完成?

python - 复制列并将结果与​​另一个数据框连接

python - Pandas 数据框到 AngularJS

git - 是否有 "git touch"以便我可以使用新的时间戳推送同一个文件?