如何在 pandas 中设置日期时间下限操作的基础? [英] How to set the base of a datetime floor operation in pandas?

查看:82
本文介绍了如何在 pandas 中设置日期时间下限操作的基础?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新分配一系列日期的时间戳,以使它们以(例如)3天的频率为基准:

I want to reassign the timestamps of a series of dates such that they get floored at a frequency of (e.g.) 3 days:

import pandas as pd

x = pd.date_range('01-01-2019', freq='1D', periods=7).floor('3D')
y = pd.date_range('01-01-2022', freq='1D', periods=7).floor('3D')

我期待地板"与第一个日期对齐并产生:

I am expecting the "floor" to align to the first date and produce:

In[3]: x
Out[3]: 
DatetimeIndex(['2019-01-01', '2019-01-01', '2019-01-01', '2019-01-04',
               '2019-01-04', '2019-01-04', '2019-01-07'],
              dtype='datetime64[ns]', freq=None)

In[4]: y
Out[4]:
DatetimeIndex(['2022-01-01', '2022-01-01', '2022-01-01', '2022-01-04',
               '2022-01-04', '2022-01-04', '2022-01-07'],
              dtype='datetime64[ns]', freq=None)

但是相反,似乎日期有一个3天的周期(大概是1970年1月1日以来3天的倍数?),所以结果是:

But instead it seems like there is a 3 day cycle the dates are floored to (presumably multiples of 3 days since Jan 1 1970?), so instead the result is:

In[3]: x
Out[3]: 
DatetimeIndex(['2018-12-30', '2019-01-02', '2019-01-02', '2019-01-02',
               '2019-01-05', '2019-01-05', '2019-01-05'],
              dtype='datetime64[ns]', freq=None)

In[4]: y
Out[4]:
DatetimeIndex(['2022-01-01', '2022-01-01', '2022-01-01', '2022-01-04',
               '2022-01-04', '2022-01-04', '2022-01-07'],
              dtype='datetime64[ns]', freq=None)

x 的结果从12月30日开始,而不是1月1日开始.

The results for x start on December 30 instead of January 1.

有没有一种方法可以设置基准"?在熊猫中进行 floor 操作??由于

Is there a way to set a "base" for the floor operation in pandas? I say "base" because of the base argument in resample for doing similar adjustments. But I don't want to do any aggregation, just keep each element but reassign the timestamp.

推荐答案

x = pd.date_range('01-01-2019', freq='1D', periods=7)
y = pd.date_range('01-01-2022', freq='1D', periods=7)

def floor(x, freq):
    offset = x[0].ceil(freq) - x[0]
    return (x + offset).floor(freq) - offset

print(floor(x, '3D'))
print(floor(y, '3D'))

输出

DatetimeIndex(['2019-01-01', '2019-01-01', '2019-01-01', '2019-01-04',
               '2019-01-04', '2019-01-04', '2019-01-07'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2022-01-01', '2022-01-01', '2022-01-01', '2022-01-04',
               '2022-01-04', '2022-01-04', '2022-01-07'],
              dtype='datetime64[ns]', freq=None)

添加加法逻辑:

def floor(x, freq):
    offset = x[0].ceil(freq) - x[0]
    adj_needed = (offset != pd.Timedelta(0))
    return (x + offset).floor(freq) - offset if adj_needed else x.floor(freq)

这篇关于如何在 pandas 中设置日期时间下限操作的基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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