pandas - 使用除午夜以外的其他开始时间分组日期

标签 pandas group-by timestamp resampling

我正在按日期汇总一些数据。

for dt,group in df.groupby(df.timestamp.dt.date):
      # do stuff

现在,我想做同样的事情,但不使用午夜作为时间偏移量。 尽管如此,我还是想使用 groupby,但是例如在早上 6 点到早上 6 点的垃圾箱中。 有没有比虚拟列更好的解决方案? 不幸的是,按照

中的讨论重新采样

Resample daily pandas timeseries with start at time other than midnight Resample hourly TimeSeries with certain starting hour

不起作用,因为我确实需要应用任何重采样/聚合函数

最佳答案

例如,您可以在分组前减去偏移量:

for dt, group in df.groupby(df.timestamp.sub(pd.to_timedelta('6H')).dt.date):
    # do stuff

关于pandas - 使用除午夜以外的其他开始时间分组日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58190176/

相关文章:

python - pandas 中 'float64' 列类型的总和返回 float 而不是 numpy.float64

python - 如何用空格转换 float 中的字符串 - pandas

Pandas 定位多个条件

c# - 使用 Linq 进行 GroupBy 和选择

mapping - 如何让 elasticsearch 为所有索引中的每个文档添加时间戳字段?

mysql - 为什么更新相同数据时 'on update CURRENT_TIMESTAMP' 不更新?

Python Pandas 连接数据帧并重命名索引

mysql - MySQL 中根据时间间隔对行进行分组

python - 在 Python Polars 中获取每个 groupby/apply 的相关性

Javascript - 将日期字符串重新格式化为 ISO8601