【问题标题】:Python: reduce precision pandas timestamp dataframePython:减少精度熊猫时间戳数据帧
【发布时间】:2023-01-31 20:02:05
【问题描述】:

您好,我有以下数据框

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

【问题讨论】:

  • Timedatetime 类型吗?
  • 如果我数字 type(df.Time[0]) 它返回 pandas.tslib.Timestamp
  • 也很高兴了解您为什么需要这个。可能是XY Problem
  • 我需要在秒级比较不同的数据帧。例如我必须做df2 = df[df.time == df1.time]之类的操作
  • 这有用吗? df2 = df[(df.time - df1.time).abs() < pd.Timedelta('1 second')]

标签: python pandas timestamp dataframe


【解决方案1】:

您可以使用 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 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]')

【讨论】:

  • pandas 只支持datetime64[ns]吗?是否有内置的方式来改变精度。说只喜欢约会?
  • @mnky9800n:目前,Pandas NDFrames 仅支持datetime64[ns]
  • 是否有支持其他日期时间的计划?我认为有明显的原因,因为 datetime64[ns] 范围只能长达 600 年。
  • 我现在得到 TypeError: Cannot cast DatetimeArray to dtype datetime64[s] 和 pandas '0.25.3'
  • 有没有内存使用量较少的东西,比如 datetime32?
【解决方案2】:

如果你真的必须删除日期时间的microsecond部分,你可以使用Timestamp.replace方法和Series.apply方法将它应用于整个系列,用0替换microsecond部分。例子 -

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

【讨论】:

    【解决方案3】:

    对于 0.24.0 或更高版本的 pandas,您只需在 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
    

    【讨论】:

    • 这是一个干净的解决方案。根据情况,roundfloor也可以。
    猜你喜欢
    • 2021-06-22
    • 2013-02-03
    • 2021-06-27
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2015-06-20
    相关资源
    最近更新 更多