Python: 降低精度 pandas 时间戳数据帧
Python: reduce precision 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
如果您真的必须删除日期时间的 microsecond
部分,您可以使用 Timestamp.replace
方法和 Series.apply
方法将其应用于整个系列,以替换 microsecond
与 0
的一部分。示例 -
df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
演示 -
In [25]: df
Out[25]:
Record_ID Time
0 94704 2014-03-10 07:19:19.647342
1 94705 2014-03-10 07:21:44.479363
2 94706 2014-03-10 07:21:45.479581
3 94707 2014-03-10 07:21:54.481588
4 94708 2014-03-10 07:21:55.481804
In [26]: type(df['Time'][0])
Out[26]: pandas.tslib.Timestamp
In [27]: df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
In [28]: df
Out[28]:
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
您可以使用 astype
:
将基础 datetime64[ns]
值转换为 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 系列和数据帧 store all datetime values as datetime64[ns]
这些 datetime64[s]
值会自动转换回 datetime64[ns]
,因此最终结果仍存储为 datetime64[ns]
值,但调用 astype
会导致秒的小数部分被删除。
如果您想要一个包含 datetime64[s]
个值的 NumPy 数组,您可以使用 df['Time'].values.astype('datetime64[s]')
.
对于pandas 0.24.0 或以上版本,您可以简单地在ceil() 函数中设置freq 参数来获得您想要的精度:
df['Time'] = df.Time.dt.ceil(freq='s')
In [28]: df
Out[28]:
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
您好,我有以下数据框
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
如果您真的必须删除日期时间的 microsecond
部分,您可以使用 Timestamp.replace
方法和 Series.apply
方法将其应用于整个系列,以替换 microsecond
与 0
的一部分。示例 -
df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
演示 -
In [25]: df
Out[25]:
Record_ID Time
0 94704 2014-03-10 07:19:19.647342
1 94705 2014-03-10 07:21:44.479363
2 94706 2014-03-10 07:21:45.479581
3 94707 2014-03-10 07:21:54.481588
4 94708 2014-03-10 07:21:55.481804
In [26]: type(df['Time'][0])
Out[26]: pandas.tslib.Timestamp
In [27]: df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
In [28]: df
Out[28]:
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
您可以使用 astype
:
datetime64[ns]
值转换为 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 系列和数据帧 store all datetime values as datetime64[ns]
这些 datetime64[s]
值会自动转换回 datetime64[ns]
,因此最终结果仍存储为 datetime64[ns]
值,但调用 astype
会导致秒的小数部分被删除。
如果您想要一个包含 datetime64[s]
个值的 NumPy 数组,您可以使用 df['Time'].values.astype('datetime64[s]')
.
对于pandas 0.24.0 或以上版本,您可以简单地在ceil() 函数中设置freq 参数来获得您想要的精度:
df['Time'] = df.Time.dt.ceil(freq='s')
In [28]: df
Out[28]:
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