Python: reduce precision pandas timestamp dataframe(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
推荐答案
你可以转换底层 datetime64[ns]
值使用 astype
转换为 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 系列和 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 时间戳数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python:降低精度 pandas 时间戳数据帧


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01